_roles = $roles; $this->_allRoles = $allRoles; } // // Implement template methods from AuthorizationPolicy // /** * @see AuthorizationPolicy::effect() */ public function effect() { // Check whether the user has one of the allowed roles // assigned. If that's the case we'll permit access. // Get user roles grouped by context. $userRoles = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_USER_ROLES); if (empty($userRoles)) { return AuthorizationPolicy::AUTHORIZATION_DENY; } if (!$this->_checkUserRoleAssignment($userRoles)) { return AuthorizationPolicy::AUTHORIZATION_DENY; } if (!$this->_checkOperationWhitelist()) { return AuthorizationPolicy::AUTHORIZATION_DENY; } $handler = $this->getRequest()->getRouter()->getHandler(); $handler->markRoleAssignmentsChecked(); return AuthorizationPolicy::AUTHORIZATION_PERMIT; } // // Private helper methods // /** * Check whether the given user has been assigned * to any of the allowed roles. If so then grant * access. * * @param array $userRoles * * @return bool */ public function _checkUserRoleAssignment($userRoles) { // Find matching roles. $foundMatchingRole = false; foreach ($this->_roles as $roleId) { $foundMatchingRole = in_array($roleId, $userRoles); if ($this->_allRoles) { if (!$foundMatchingRole) { // When the "all roles" flag is switched on then // one missing role is enough to fail. return false; } } else { if ($foundMatchingRole) { // When the "all roles" flag is not set then // one matching role is enough to succeed. return true; } } } if ($this->_allRoles) { // All roles matched, otherwise we'd have failed before. return true; } else { // None of the roles matched, otherwise we'd have succeeded already. return false; } } } if (!PKP_STRICT_MODE) { class_alias('\PKP\security\authorization\RoleBasedHandlerOperationPolicy', '\RoleBasedHandlerOperationPolicy'); }