// *************************************************************** // Function: GroupPermissions.cpp // *************************************************************** #include "GroupPermissions.h" #include "Main.h" //************************************ // Name: ~GroupPermissions // // Class deconstructor // // Parameters: // // Return: // //************************************ GroupPermissions::~GroupPermissions() { list::iterator itr; for (itr=mGroups.begin(); itr!=mGroups.end(); itr++) { (*itr)->allowed.clear(); (*itr)->disallowed.clear(); delete *itr; } mGroups.clear(); } GroupPermissions::GroupPermissions() { } //************************************ // Name: LoadGroups // // Loads the permissions group table from the database. // // Parameters: // // Return: // - true //************************************ bool GroupPermissions::LoadGroups() { //Clear previous groups mGroups.clear(); //Get the permission group rows char query[130]; sprintf(query, "SELECT GroupID, GroupName, AllowedCommands, DisallowedCommands FROM group_permissions"); unsigned long query_reference=gDatabase.Query(query, (long)strlen(query)); QUERY_RESULT* pQuery_result=NULL; //Update the database until a the result of our query is available while((pQuery_result=gDatabase.StealResult(query_reference))==NULL) { gDatabase.Update(); } int num_records=(int)mysql_num_rows(pQuery_result->pResult); for (int i=0; ipResult); pPermGroup->groupId=atoi(row[0]); strcpy(pPermGroup->groupName, row[1]); //Extract the allowed commands char* temp=strtok(row[2], ", "); while(temp!=NULL) { REST_COMMAND r; strcpy(r.command, temp); pPermGroup->allowed.push_back(r); temp=strtok(NULL, ", "); } //extract the disallowed commands temp=strtok(row[3], ", "); while(temp!=NULL) { REST_COMMAND r; strcpy(r.command, temp); pPermGroup->disallowed.push_back(r); temp=strtok(NULL, ", "); } pPermGroup->allowedAll=false; if (strcmp(pPermGroup->allowed.front().command, "all")==0) { pPermGroup->allowedAll=true; } mGroups.push_back(pPermGroup); } gCoreLog.notice("Loaded: %d permission groups.", mGroups.size()); gDatabase.FreeResult(query_reference); return true; } //************************************ // Name: HasPermission // // Checks if the specified group has permission to execute the command. // // Parameters: // groupID - Document // pCommand - Document // // Return: // true - Command is allowed to be executed // false - Command not allowed //************************************ bool GroupPermissions::HasPermission(char groupId, char* pCommand) { //If the groupID is zero, we deny access to the command without a second thought =D if (groupId==0){return false;} if (groupId==100){return true;} //Find the group list::iterator itr; for (itr=mGroups.begin(); itr!=mGroups.end(); itr++) { if ((*itr)->groupId==groupId) { //Check the allowed commands if (!(*itr)->allowedAll) { list::iterator itr2; for (itr2=(*itr)->allowed.begin(); itr2!=(*itr)->allowed.end(); itr2++) { if (strcmp(itr2->command, pCommand)==0) { return true; } } return false; } //Check the disallowed commands list::iterator itr2; for (itr2=(*itr)->disallowed.begin(); itr2!=(*itr)->disallowed.end(); itr2++) { if (strcmp(itr2->command, pCommand)==0) { return false; } } return true; } } return false; }