Example #1
0
 public static function modifyRole($roleId, $roleName, $accessList, $userId)
 {
     try {
         $db = Database::getInstance();
         $conn = $db->getConnection();
         HicreteLogger::logInfo("Modifying role");
         if (configutils::isRoleAvailableForModify($roleName, $roleId)) {
             $conn->beginTransaction();
             $stmt = $conn->prepare("SELECT * FROM `rolemaster` WHERE `roleId`=:roleId");
             $stmt->bindParam(':roleId', $roleId, PDO::PARAM_STR);
             $rollback = true;
             HicreteLogger::logDebug("Query:\n " . json_encode($stmt));
             if ($stmt->execute()) {
                 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
                 if (count($result) <= 0) {
                     echo AppUtil::getReturnStatus("Unsuccessful", "Role not found");
                     return;
                 } else {
                     $stmt = $conn->prepare("DELETE FROM `roleaccesspermission` WHERE `roleId`=:roleId");
                     $stmt->bindParam(':roleId', $roleId, PDO::PARAM_STR);
                     HicreteLogger::logDebug("Query:\n " . json_encode($stmt));
                     if ($stmt->execute()) {
                         $stmt = $conn->prepare("UPDATE `rolemaster` SET `roleName`=:roleName , `lastModifiedBy`=:userId,`lasModificationDate`=now() WHERE `roleId`=:roleId");
                         $stmt->bindParam(':roleId', $roleId, PDO::PARAM_STR);
                         $stmt->bindParam(':userId', $userId, PDO::PARAM_STR);
                         $stmt->bindParam(':roleName', $roleName, PDO::PARAM_STR);
                         HicreteLogger::logDebug("Query:\n " . json_encode($stmt));
                         if ($stmt->execute()) {
                             $isBreak = false;
                             foreach ($accessList as $accessEntry) {
                                 if ($accessEntry->read->val) {
                                     if (!Config::insertAccessPermissionForRole($stmt, $conn, $roleId, $userId, $accessEntry->read->accessId)) {
                                         $isBreak = true;
                                         break;
                                     }
                                 }
                                 if ($accessEntry->write->val) {
                                     if (!Config::insertAccessPermissionForRole($stmt, $conn, $roleId, $userId, $accessEntry->write->accessId)) {
                                         $isBreak = true;
                                         break;
                                     }
                                 }
                             }
                             if (!$isBreak) {
                                 $rollback = false;
                             }
                         }
                     }
                 }
             }
             if ($rollback) {
                 $conn->rollback();
                 HicreteLogger::logError("Unknown databse error occured");
                 echo AppUtil::getReturnStatus("Unsuccessful", "Unknown database error occurred");
             } else {
                 $conn->commit();
                 HicreteLogger::logInfo("Role modified successfully");
                 echo AppUtil::getReturnStatus("Successful", "Role Modified successfully");
             }
         } else {
             HicreteLogger::logError("Role name already exists");
             echo AppUtil::getReturnStatus("Unsuccessful", "Role name Already Exists");
         }
     } catch (Exception $e) {
         HicreteLogger::logFatal("Exception Occured Message:\n" . $e->getMessage());
         echo AppUtil::getReturnStatus("Exception", "Exception Occurred while creating role");
     }
 }
Example #2
0
 public static function addRole($data, $userId)
 {
     try {
         $db = Database::getInstance();
         $conn = $db->getConnection();
         HicreteLogger::logInfo("Creating role");
         if (config::isRoleAvailable($data->roleName)) {
             $conn->beginTransaction();
             $roleId = AppUtil::generateId();
             $stmt = $conn->prepare("INSERT INTO `rolemaster`(`roleId`, `roleName`, `createdBy`, `creationDate`)\n                    VALUES (:roleId,:roleName,:createdBy,now())");
             $stmt->bindParam(':roleId', $roleId, PDO::PARAM_STR);
             $stmt->bindParam(':roleName', $data->roleName, PDO::PARAM_STR);
             $stmt->bindParam(':createdBy', $userId, PDO::PARAM_STR);
             $rollback = false;
             HicreteLogger::logDebug("Query:\n " . json_encode($stmt));
             if ($stmt->execute()) {
                 foreach ($data->accessPermissions as $accessEntry) {
                     if ($accessEntry->read->val) {
                         if (!Config::insertAccessPermissionForRole($stmt, $conn, $roleId, $userId, $accessEntry->read->accessId)) {
                             $rollback = true;
                         }
                     }
                     if ($accessEntry->write->val) {
                         if (!Config::insertAccessPermissionForRole($stmt, $conn, $roleId, $userId, $accessEntry->write->accessId)) {
                             $rollback = true;
                         }
                     }
                 }
             } else {
                 HicreteLogger::logError("Role addition failed");
                 echo AppUtil::getReturnStatus("Unsuccessful", "Unknown database error occurred");
             }
             if ($rollback) {
                 $conn->rollback();
                 HicreteLogger::logError("Role addition failed");
                 echo AppUtil::getReturnStatus("Unsuccessful", "Unknown database error occurred");
             } else {
                 $conn->commit();
                 HicreteLogger::logInfo("Role Created successfully");
                 echo AppUtil::getReturnStatus("Successful", "Role created successfully");
             }
         } else {
             HicreteLogger::logError("Role is already available");
             echo AppUtil::getReturnStatus("Unsuccessful", "Role is Already Available");
         }
     } catch (Exception $e) {
         HicreteLogger::logFatal("Exception Occured Message:\n" . $e->getMessage());
         echo AppUtil::getReturnStatus("Exception", "Exception Occurred while creating role");
     }
 }