Пример #1
0
 protected function addPermission($permName, $user = null, $site = null)
 {
     if ($user == null) {
         $user = $this->user;
     }
     if ($site == null) {
         $site = $this->site;
     }
     $role = \Bazalt\Auth\Model\Role::create();
     $role->title = $permName . ' test role' . $user->id;
     $role->save();
     $this->models[] = $role;
     $perm = \Bazalt\Auth\Model\Permission::getById($permName);
     if (!$perm) {
         $perm = new \Bazalt\Auth\Model\Permission();
         $perm->id = $permName;
         $perm->save();
         $this->models[] = $perm;
     }
     $role->Permissions->add($perm);
     $user->Roles->add($role, ['site_id' => $site->id]);
 }
Пример #2
0
 public function testSwitchRole()
 {
     \Bazalt\Site\Option::set(\Bazalt\Auth::SPLIT_ROLES_OPTION, false);
     // create role
     $role = Role::create();
     $role->title = 'Test1';
     $role->save();
     $this->models[] = $role;
     // create role
     $role2 = Role::create();
     $role2->title = 'Test2';
     $role2->save();
     $this->models[] = $role2;
     $this->user->Roles->add($role, ['site_id' => $this->site->id]);
     $this->user->Roles->add($role2, ['site_id' => $this->site->id]);
     //        print_r($this->user->getRoles());
     $curRole = \Bazalt\Auth::getCurrentRole();
     //        print_r($curRole);
     $this->assertEquals($role->id, $curRole->id);
     $this->assertTrue(\Bazalt\Auth::setCurrentRole($role->id));
     $curRole = \Bazalt\Auth::getCurrentRole();
     $this->assertEquals($role->id, $curRole->id);
     $this->assertTrue(\Bazalt\Auth::setCurrentRole($role2->id));
     $curRole = \Bazalt\Auth::getCurrentRole();
     $this->assertEquals($role2->id, $curRole->id);
     $this->assertFalse(\Bazalt\Auth::setCurrentRole(9999));
     //try to set non exists role
     $curRole = \Bazalt\Auth::getCurrentRole();
     $this->assertEquals($role2->id, $curRole->id);
     \Bazalt\Site\Option::set(\Bazalt\Auth::SPLIT_ROLES_OPTION, true);
 }