Exemplo n.º 1
0
function distributeRoles($requestingUserId, $userId, $sharedFoldersArray, $startedTransaction = false)
{
    /*
    This function will create roles or apply new roles for a user to a folder's lists. It will not create a folder role. It performs checks to verify the requesting user has sufficient roles for the folders. It is not used for lists outside of folders as there is no way of knowing which users to add. That's the reason why folders exist.
    $requestingUserId = (int) the user responsible for the request to change roles.
    $userId = (int) the user whose roles will be affected.
    $sharedFoldersArray = (array) in the format of array(folderId => folderRoleId, folderId => folderRoleId,...).
    Returns (boolean) true on success, false on failure. Use === true or === false to verify success or failure.
    */
    global $debug, $message, $Dbc;
    $requestingUserId = intval($requestingUserId);
    $userId = intval($userId);
    if (!empty($sharedFoldersArray) && !is_array($sharedFoldersArray)) {
        $debug->add('The $sharedFoldersArray parameter is empty or not an array:');
        $debug->printArray($sharedFoldersArray, '$sharedFoldersArray');
        $message .= error(__LINE__, false, 'From distributeRoles() on line ' . __LINE__ . ' in functions.php.<br>');
        return false;
    } else {
        try {
            if (!$startedTransaction) {
                $Dbc->beginTransaction();
            }
            //Prepare the folder role check query.
            $folderRoleCheck = $Dbc->prepare("SELECT\n\tfolderRoleId AS 'folderRoleId'\nFROM\n\tuserFolderSettings\nWHERE\n\tuserId = ? AND\n\tfolderId = ?");
            //Prepare the update folder role query.
            $updateFolderRole = $Dbc->prepare("UPDATE\n\tuserFolderSettings\nSET\n\tfolderRoleId = ?\nWHERE\n\tuserId = ? AND\n\tfolderId = ?\nLIMIT 1");
            //Prepare the get folder lists query.
            $getListsQuery = $Dbc->prepare("SELECT\n\tlists.listId AS 'listId'\nFROM\n\tlists\nWHERE\n\tfolderId = ?");
            $sharedLists = array();
            //Loop through the folders.
            foreach ($sharedFoldersArray as $folderId => $folderRoleId) {
                //Verify the requesting user has a sufficient folder role.
                $folderRoleCheck->execute(array($requestingUserId, $folderId));
                $requestingUserRole = $folderRoleCheck->fetch(PDO::FETCH_ASSOC);
                if ($requestingUserRole['folderRoleId'] < 3) {
                    //The user does not have a sufficient folder role.
                    //Get the name of the folder.
                    $folderInfo = getFolderInfo($requestingUserId, $folderId);
                    $folderName = $folderInfo['folderName'];
                    $message .= $folderName ? 'Could not update the role for the folder "' . $folderName . '".<br>' : 'Could not update the role for a folder.<br>';
                    $debug->add('Could not get the folderName for folderId: ' . $folderId . '. The requesting userId is: ' . $requestingUserId . '.');
                    continue;
                }
                //Update the folder role.
                $params = array($folderRoleId, $userId, $folderId);
                $updateFolderRole->execute($params);
                $getListsQuery->execute(array($folderId));
                while ($temp = $getListsQuery->fetch(PDO::FETCH_ASSOC)) {
                    //Fill the array with the format: listId => array('userListRoleId' => 3, 'requestingUserRoleId' => 4).
                    $sharedLists[$temp['listId']] = array('userListRoleId' => $folderRoleId, 'requestingUserRoleId' => $requestingUserRole['folderRoleId']);
                }
            }
            $debug->printArray($sharedLists, '$sharedLists');
            //See if the user has an existing role for the lists.
            $existingListRoleQuery = $Dbc->prepare("SELECT\n\tlistRoleId AS 'listRoleId'\nFROM\n\tuserListSettings\nWHERE\n\tuserId = ? AND\n\tlistId = ?");
            foreach ($sharedLists as $listId => $listInfo) {
                $params = array($userId, $listId);
                $existingListRoleQuery->execute($params);
                $existingListRole = $existingListRoleQuery->fetch(PDO::FETCH_ASSOC);
                if ($existingListRole['listRoleId'] === '' || $existingListRole['listRoleId'] === NULL) {
                    //The user does not have an existing role in the list. Insert one.
                    $insertListRole = $Dbc->prepare("INSERT INTO\n\tuserListSettings\nSET\n\tuserId = ?,\n\tlistId = ?,\n\tlistRoleId = ?,\n\tdateAdded = ?");
                    $params = array($userId, $listId, $listInfo['userListRoleId'], DATETIME);
                    $insertListRole->execute($params);
                } else {
                    //The user has an existing list role. Update it.
                    if ($listInfo['requestingUserRoleId'] == 3 && $existingListRole >= 3) {
                        //Managers cannot change the role of fellow Managers or Owners.
                        $message .= 'One or more list roles could not be updated. You cannot change the role of a Manager or Owner.<br>';
                    } else {
                        $updateListRole = $Dbc->prepare("UPDATE\n\tuserListSettings\nSET\n\tlistRoleId = ?\nWHERE\n\tuserId = ? AND\n\tlistId = ?");
                        $params = array($listInfo['userListRoleId'], $userId, $listId);
                        $updateListRole->execute($params);
                    }
                }
            }
            if (!$startedTransaction) {
                $Dbc->commit();
            }
            return true;
        } catch (PDOException $e) {
            $message .= error(__LINE__, '', '<pre>' . $e . '</pre>');
            return false;
        }
    }
}
Exemplo n.º 2
0
function updatePendingRole()
{
    //Update the pending user's role id. Invitations are handled in one database table, so one function can handle both.
    global $debug, $message, $success, $Dbc, $returnThis;
    $output = '';
    try {
        if (empty($_POST['invitationId'])) {
            throw new Adrlist_CustomException('', '$_POST[\'invitationId\'] is empty.');
        } elseif (empty($_POST['type'])) {
            throw new Adrlist_CustomException('', '$_POST[\'type\'] is empty.');
        } elseif (empty($_POST['typeId'])) {
            throw new Adrlist_CustomException('', '$_POST[\'typeId\'] is empty.');
        } elseif (!isset($_POST['newRoleId'])) {
            //The newRoleId may be zero, so check that the value isset rather than empty.
            throw new Adrlist_CustomException('', '$_POST[\'newRoleId\'] is not set.');
        }
        if ($_POST['type'] == 'list') {
            $type = 'list';
            $listInfo = getListInfo($_SESSION['userId'], $_POST['typeId']);
            $role = $listInfo['listRoleId'];
        } else {
            $type = 'folder';
            $folderInfo = getFolderInfo($_SESSION['userId'], $_POST['typeId']);
            $role = $folderInfo['folderRoleId'];
        }
        //Verify the user has a sufficient role to delete invitations.
        if (empty($role) || $role < 3) {
            throw new Adrlist_CustomException('Your role does not allow you to edit this ' . $_POST['type'] . '.', '');
        }
        //Update the roleId.
        $stmt = $Dbc->prepare("UPDATE\n\tinvitations\nSET\n\t{$type}RoleId = ?\nWHERE\n\tinvitationId = ?\nLIMIT 1");
        $params = array($_POST['newRoleId'], $_POST['invitationId']);
        $stmt->execute($params);
        $rowCount = $stmt->rowCount();
        if (empty($rowCount)) {
            pdoError(__LINE__, $stmt, $params, true);
        }
        //Get the id of the folder or list to pass to the buildUser functions.
        $getIdQuery = $Dbc->prepare("SELECT\n\t{$type}Id AS '{$type}Id'\nFROM\n\tinvitations\nWHERE\n\tinvitationId = ?");
        $getIdQuery->execute(array($_POST['invitationId']));
        $row = $getIdQuery->fetch(PDO::FETCH_ASSOC);
        if ($type == 'folder') {
            $_POST['folderId'] = $row['folderId'];
            //We will not update user's role id for all of the folder's lists. That occurs when the pending user creates an account.
            $returnThis['buildUsers'] = buildFolderUsers();
        } else {
            $_POST['listId'] = $row['listId'];
            $returnThis['buildUsers'] = buildListUsers();
        }
        if (MODE == 'updatePendingRole') {
            $success = true;
            $message .= 'Updated';
        }
    } catch (Adrlist_CustomException $e) {
    } catch (PDOException $e) {
        error(__LINE__, '', '<pre>' . $e . '</pre>');
    }
    if (MODE == 'updatePendingRole') {
        returnData();
    }
}