public function testDefaultAdmin()
 {
     $adminMembers = Permission::get_members_by_permission('ADMIN');
     $this->assertEquals(0, $adminMembers->count());
     $admin = Member::default_admin();
     $this->assertInstanceOf('SilverStripe\\Security\\Member', $admin);
     $this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
     $this->assertEquals($admin->Email, Security::default_admin_username());
     $this->assertNull($admin->Password);
 }
 public function canView($member = null)
 {
     if (!$member && $member !== false) {
         $member = Member::currentUser();
     }
     // cms menus only for logged-in members
     if (!$member) {
         return false;
     }
     // Check they can access the CMS and that they are trying to edit themselves
     if (Permission::checkMember($member, "CMS_ACCESS") && $member->ID === Member::currentUserID()) {
         return true;
     }
     return false;
 }
 public function testEmptyMemberFails()
 {
     $member = new Member();
     $this->assertFalse($member->exists());
     $this->logInWithPermission('ADMIN');
     $this->assertFalse(Permission::checkMember($member, 'ADMIN'));
     $this->assertFalse(Permission::checkMember($member, 'CMS_ACCESS_LeftAndMain'));
 }
 /**
  * Returns true if the member is allowed to do the given action.
  * See {@link extendedCan()} for a more versatile tri-state permission control.
  *
  * @param string $perm The permission to be checked, such as 'View'.
  * @param Member $member The member whose permissions need checking.  Defaults to the currently logged
  * in user.
  * @param array $context Additional $context to pass to extendedCan()
  *
  * @return boolean True if the the member is allowed to do the given action
  */
 public function can($perm, $member = null, $context = array())
 {
     if (!isset($member)) {
         $member = Member::currentUser();
     }
     if (Permission::checkMember($member, "ADMIN")) {
         return true;
     }
     if ($this->getSchema()->manyManyComponent(static::class, 'Can' . $perm)) {
         if ($this->ParentID && $this->SecurityType == 'Inherit') {
             if (!($p = $this->Parent)) {
                 return false;
             }
             return $this->Parent->can($perm, $member);
         } else {
             $permissionCache = $this->uninherited('permissionCache');
             $memberID = $member ? $member->ID : 'none';
             if (!isset($permissionCache[$memberID][$perm])) {
                 if ($member->ID) {
                     $groups = $member->Groups();
                 }
                 $groupList = implode(', ', $groups->column("ID"));
                 // TODO Fix relation table hardcoding
                 $query = new SQLSelect("\"Page_Can{$perm}\".PageID", array("\"Page_Can{$perm}\""), "GroupID IN ({$groupList})");
                 $permissionCache[$memberID][$perm] = $query->execute()->column();
                 if ($perm == "View") {
                     // TODO Fix relation table hardcoding
                     $query = new SQLSelect("\"SiteTree\".\"ID\"", array("\"SiteTree\"", "LEFT JOIN \"Page_CanView\" ON \"Page_CanView\".\"PageID\" = \"SiteTree\".\"ID\""), "\"Page_CanView\".\"PageID\" IS NULL");
                     $unsecuredPages = $query->execute()->column();
                     if ($permissionCache[$memberID][$perm]) {
                         $permissionCache[$memberID][$perm] = array_merge($permissionCache[$memberID][$perm], $unsecuredPages);
                     } else {
                         $permissionCache[$memberID][$perm] = $unsecuredPages;
                     }
                 }
                 Config::inst()->update($this->class, 'permissionCache', $permissionCache);
             }
             if ($permissionCache[$memberID][$perm]) {
                 return in_array($this->ID, $permissionCache[$memberID][$perm]);
             }
         }
     } else {
         return parent::can($perm, $member);
     }
 }
 public function can($perm, $member = null, $context = array())
 {
     $perms = ["PERM_{$perm}", 'CAN_ALL'];
     return Permission::checkMember($member, $perms);
 }
 /**
  * Default permissions for this ChangeSetItem
  *
  * @param string $perm
  * @param Member $member
  * @param array $context
  * @return bool
  */
 public function can($perm, $member = null, $context = array())
 {
     if (!$member) {
         $member = Member::currentUser();
     }
     // Allow extensions to bypass default permissions, but only if
     // each change can be individually published.
     $extended = $this->extendedCan($perm, $member, $context);
     if ($extended !== null) {
         return $extended;
     }
     // Default permissions
     return (bool) Permission::checkMember($member, ChangeSet::config()->required_permission);
 }
 /**
  * @param Member $member
  * @return boolean
  */
 public function canView($member = null)
 {
     if (!$member && $member !== FALSE) {
         $member = Member::currentUser();
     }
     // cms menus only for logged-in members
     if (!$member) {
         return false;
     }
     // alternative extended checks
     if ($this->hasMethod('alternateAccessCheck')) {
         $alternateAllowed = $this->alternateAccessCheck();
         if ($alternateAllowed === false) {
             return false;
         }
     }
     // Check for "CMS admin" permission
     if (Permission::checkMember($member, "CMS_ACCESS_LeftAndMain")) {
         return true;
     }
     // Check for LeftAndMain sub-class permissions
     $codes = $this->getRequiredPermissions();
     if ($codes === false) {
         // allow explicit FALSE to disable subclass check
         return true;
     }
     foreach ((array) $codes as $code) {
         if (!Permission::checkMember($member, $code)) {
             return false;
         }
     }
     return true;
 }
 /**
  * Require basic authentication.  Will request a username and password if none is given.
  *
  * Used by {@link Controller::init()}.
  *
  * @throws HTTPResponse_Exception
  *
  * @param string $realm
  * @param string|array $permissionCode Optional
  * @param boolean $tryUsingSessionLogin If true, then the method with authenticate against the
  *  session log-in if those credentials are disabled.
  * @return Member|bool $member
  */
 public static function requireLogin($realm, $permissionCode = null, $tryUsingSessionLogin = true)
 {
     $isRunningTests = class_exists('SilverStripe\\Dev\\SapphireTest', false) && SapphireTest::is_running_test();
     if (!Security::database_is_ready() || Director::is_cli() && !$isRunningTests) {
         return true;
     }
     /*
      * Enable HTTP Basic authentication workaround for PHP running in CGI mode with Apache
      * Depending on server configuration the auth header may be in HTTP_AUTHORIZATION or
      * REDIRECT_HTTP_AUTHORIZATION
      *
      * The follow rewrite rule must be in the sites .htaccess file to enable this workaround
      * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
      */
     $authHeader = isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] : (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) ? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] : null);
     $matches = array();
     if ($authHeader && preg_match('/Basic\\s+(.*)$/i', $authHeader, $matches)) {
         list($name, $password) = explode(':', base64_decode($matches[1]));
         $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
         $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
     }
     $member = null;
     if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
         $member = MemberAuthenticator::authenticate(array('Email' => $_SERVER['PHP_AUTH_USER'], 'Password' => $_SERVER['PHP_AUTH_PW']), null);
     }
     if (!$member && $tryUsingSessionLogin) {
         $member = Member::currentUser();
     }
     // If we've failed the authentication mechanism, then show the login form
     if (!$member) {
         $response = new HTTPResponse(null, 401);
         $response->addHeader('WWW-Authenticate', "Basic realm=\"{$realm}\"");
         if (isset($_SERVER['PHP_AUTH_USER'])) {
             $response->setBody(_t('BasicAuth.ERRORNOTREC', "That username / password isn't recognised"));
         } else {
             $response->setBody(_t('BasicAuth.ENTERINFO', "Please enter a username and password."));
         }
         // Exception is caught by RequestHandler->handleRequest() and will halt further execution
         $e = new HTTPResponse_Exception(null, 401);
         $e->setResponse($response);
         throw $e;
     }
     if ($permissionCode && !Permission::checkMember($member->ID, $permissionCode)) {
         $response = new HTTPResponse(null, 401);
         $response->addHeader('WWW-Authenticate', "Basic realm=\"{$realm}\"");
         if (isset($_SERVER['PHP_AUTH_USER'])) {
             $response->setBody(_t('BasicAuth.ERRORNOTADMIN', "That user is not an administrator."));
         }
         // Exception is caught by RequestHandler->handleRequest() and will halt further execution
         $e = new HTTPResponse_Exception(null, 401);
         $e->setResponse($response);
         throw $e;
     }
     return $member;
 }
示例#9
0
 /**
  * Check if this file can be modified
  *
  * @param Member $member
  * @return boolean
  */
 public function canEdit($member = null)
 {
     if (!$member) {
         $member = Member::currentUser();
     }
     $result = $this->extendedCan('canEdit', $member);
     if ($result !== null) {
         return $result;
     }
     return Permission::checkMember($member, array('CMS_ACCESS_AssetAdmin', 'CMS_ACCESS_LeftAndMain'));
 }
示例#10
0
 /**
  * Users can edit their own record.
  * Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions
  *
  * @param Member $member
  * @return bool
  */
 public function canDelete($member = null)
 {
     if (!$member instanceof Member) {
         $member = Member::currentUser();
     }
     //check for extensions, we do this first as they can overrule everything
     $extended = $this->extendedCan(__FUNCTION__, $member);
     if ($extended !== null) {
         return $extended;
     }
     //need to be logged in and/or most checks below rely on $member being a Member
     if (!$member) {
         return false;
     }
     // Members are not allowed to remove themselves,
     // since it would create inconsistencies in the admin UIs.
     if ($this->ID && $member->ID == $this->ID) {
         return false;
     }
     // HACK: if you want to delete a member, you have to be a member yourself.
     // this is a hack because what this should do is to stop a user
     // deleting a member who has more privileges (e.g. a non-Admin deleting an Admin)
     if (Permission::checkMember($this, 'ADMIN')) {
         if (!Permission::checkMember($member, 'ADMIN')) {
             return false;
         }
     }
     //standard check
     return Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin');
 }
 /**
  * @param Member $member
  * @return boolean
  */
 public function canView($member = null)
 {
     if (!$member && $member !== FALSE) {
         $member = Member::currentUser();
     }
     // cms menus only for logged-in members
     if (!$member) {
         return false;
     }
     // alternative extended checks
     if ($this->hasMethod('alternateAccessCheck')) {
         $alternateAllowed = $this->alternateAccessCheck();
         if ($alternateAllowed === FALSE) {
             return false;
         }
     }
     // Check for "CMS admin" permission
     if (Permission::checkMember($member, "CMS_ACCESS_LeftAndMain")) {
         return true;
     }
     // Check for LeftAndMain sub-class permissions
     $codes = array();
     $extraCodes = $this->stat('required_permission_codes');
     if ($extraCodes !== false) {
         // allow explicit FALSE to disable subclass check
         if ($extraCodes) {
             $codes = array_merge($codes, (array) $extraCodes);
         } else {
             $codes[] = "CMS_ACCESS_{$this->class}";
         }
     }
     foreach ($codes as $code) {
         if (!Permission::checkMember($member, $code)) {
             return false;
         }
     }
     return true;
 }
 /**
  * Determine if the current user is able to set the given site stage / archive
  *
  * @param SS_HTTPRequest $request
  * @return bool
  */
 public static function can_choose_site_stage($request)
 {
     // Request is allowed if stage isn't being modified
     if ((!$request->getVar('stage') || $request->getVar('stage') === static::LIVE) && !$request->getVar('archiveDate')) {
         return true;
     }
     // Check permissions with member ID in session.
     $member = Member::currentUser();
     $permissions = Config::inst()->get(get_called_class(), 'non_live_permissions');
     return $member && Permission::checkMember($member, $permissions);
 }
 /**
  * Checks for permission-code CMS_ACCESS_SecurityAdmin.
  *
  * @param $member Member
  * @return boolean
  */
 public function canView($member = null)
 {
     if (!$member || !is_a($member, 'SilverStripe\\Security\\Member') || is_numeric($member)) {
         $member = Member::currentUser();
     }
     // extended access checks
     $results = $this->extend('canView', $member);
     if ($results && is_array($results)) {
         if (!min($results)) {
             return false;
         }
     }
     // user needs access to CMS_ACCESS_SecurityAdmin
     if (Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")) {
         return true;
     }
     return false;
 }