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);
 }
 /**
  * Ensure the the get_*_by_permission functions are permission role aware
  */
 public function testGettingMembersByPermission()
 {
     $accessMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'access');
     $accessAuthor = $this->objFromFixture('SilverStripe\\Security\\Member', 'author');
     $result = Permission::get_members_by_permission(array('CMS_ACCESS_SecurityAdmin'));
     $resultIDs = $result ? $result->column() : array();
     $this->assertContains($accessMember->ID, $resultIDs, 'Member is found via a permission attached to a role');
     $this->assertNotContains($accessAuthor->ID, $resultIDs);
 }
Ejemplo n.º 3
0
 /**
  * Return an existing member with administrator privileges, or create one of necessary.
  *
  * Will create a default 'Administrators' group if no group is found
  * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions
  * if no existing Member with these permissions is found.
  *
  * Important: Any newly created administrator accounts will NOT have valid
  * login credentials (Email/Password properties), which means they can't be used for login
  * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}.
  *
  * @return Member
  */
 public static function findAnAdministrator()
 {
     // coupling to subsites module
     $origSubsite = null;
     if (is_callable('Subsite::changeSubsite')) {
         $origSubsite = \Subsite::currentSubsiteID();
         \Subsite::changeSubsite(0);
     }
     $member = null;
     // find a group with ADMIN permission
     $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
     if (is_callable('Subsite::changeSubsite')) {
         \Subsite::changeSubsite($origSubsite);
     }
     if ($adminGroup) {
         $member = $adminGroup->Members()->First();
     }
     if (!$adminGroup) {
         Group::singleton()->requireDefaultRecords();
         $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
     }
     if (!$member) {
         Member::singleton()->requireDefaultRecords();
         $member = Permission::get_members_by_permission('ADMIN')->first();
     }
     if (!$member) {
         $member = Member::default_admin();
     }
     if (!$member) {
         // Failover to a blank admin
         $member = Member::create();
         $member->FirstName = _t('Member.DefaultAdminFirstname', 'Default Admin');
         $member->write();
         // Add member to group instead of adding group to member
         // This bypasses the privilege escallation code in Member_GroupSet
         $adminGroup->DirectMembers()->add($member);
     }
     return $member;
 }