コード例 #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // create admin user
     $root = factory(App\User::class)->create(['name' => 'Administrator', 'email' => '*****@*****.**', 'password' => bcrypt('123456'), 'username' => 'admin', 'location' => 'Da Nang', 'country' => 'Viet Nam', 'biography' => 'Dev', 'occupation' => 'Dev', 'website' => 'greenglobal.vn', 'image' => 'avatar.jpg']);
     // create default roles
     $admin = new Role();
     $admin->name = 'admin';
     $admin->display_name = 'Administrator';
     $admin->description = 'User is allowed to manage all system.';
     $admin->save();
     // attach roles
     $root->attachRole($admin);
 }
コード例 #2
0
 public function registerValidators()
 {
     Validator::extend('rolePermission', function ($attribute, $array) {
         $flag = true;
         if (is_array($array)) {
             foreach ($array as $value) {
                 if ($attribute == "roles") {
                     if ($value == '@') {
                         continue;
                     }
                     $check = Role::where('name', $value);
                 } else {
                     $check = Permission::where('name', $value);
                 }
                 if (!$check->count()) {
                     $flag = false;
                     break;
                 }
             }
         } else {
             $flag = false;
         }
         return $flag;
     }, 'Roles or permissions are invalid.');
 }
コード例 #3
0
 public function testUserHavePermission()
 {
     // create role creator
     $creator = new Role();
     $creator->name = 'creator';
     $creator->save();
     // create permission
     $createPost = new Permission();
     $createPost->name = 'create-post';
     $createPost->save();
     $creator->attachPermission($createPost);
     $user = factory(App\User::class)->create();
     $user->attachRole($creator);
     Auth::login($user);
     $res = $this->call('POST', '/posts');
     $this->assertEquals(200, $res->getStatusCode());
 }
コード例 #4
0
 public function testRouteRequirePermissionUserHavePermission()
 {
     RoutePermission::setRoutePermissions('POST /blog/{id}', ['create-blog']);
     // create role creator
     $creator = new Role();
     $creator->name = 'creator';
     $creator->save();
     // create permission
     $createPost = new Permission();
     $createPost->name = 'create-blog';
     $createPost->save();
     $creator->attachPermission($createPost);
     $user = factory(App\User::class)->create(['password' => bcrypt('123456')]);
     $user->attachRole($creator);
     $credentials = ['email' => $user->email, 'password' => '123456'];
     $token = JWTAuth::attempt($credentials);
     $res = $this->call('POST', '/blog/1', [], [], [], ['HTTP_Authorization' => "Bearer {$token}"]);
     $this->assertEquals(200, $res->getStatusCode());
 }
コード例 #5
0
 public function testPermissionUserIsAdmin()
 {
     // Check user is admin
     $user = factory(App\User::class)->create();
     $login = Auth::login($user);
     $admin = Role::find(1);
     // Attach admin role for user
     $user->attachRole($admin);
     $controller = new Controller();
     $isAdmin = $controller->checkPermission('manage-user');
     $this->assertEquals(true, $isAdmin);
 }
コード例 #6
0
ファイル: Role.php プロジェクト: wyrover/laravel-users
 /**
  * Browse items
  *
  * @param  array  $options
  * @return array
  */
 public static function browse($options = [])
 {
     $find = new Role();
     $fillable = $find->fillable;
     $total = $find->count();
     if (!empty($options['order'])) {
         foreach ($options['order'] as $field => $direction) {
             if (in_array($field, $fillable)) {
                 $find = $find->orderBy($field, $direction);
             }
             $find = $find->orderBy('id', 'DESC');
         }
     }
     if (!empty($options['offset'])) {
         $find = $find->skip($options['offset']);
     }
     if (!empty($options['limit'])) {
         $find = $find->take($options['limit']);
     }
     return ['total' => $total, 'offset' => empty($options['offset']) ? 0 : $options['offset'], 'limit' => empty($options['limit']) ? 0 : $options['limit'], 'data' => $find->get()];
 }
コード例 #7
0
 public function assignRole($id, Request $request)
 {
     $user = AppUser::find($id);
     if (!$user) {
         return response()->json(null, 404);
     }
     $roleIdOrName = $request->roleIdOrName;
     $field = is_numeric($roleIdOrName) ? 'id' : 'name';
     $role = Role::where($field, $roleIdOrName)->first();
     if (!$role) {
         return response()->json(arrayView('phpsoft.users::errors/validation', ['errors' => ['Role does not exist.']]), 400);
     }
     $hasRole = $user->hasRole($role->name);
     if ($hasRole) {
         return response()->json(null, 204);
     }
     $user->attachRole($role);
     return response()->json(null, 204);
 }
コード例 #8
0
 public function testBrowseWithOrderRightParams()
 {
     $roles = [];
     for ($i = 0; $i < 10; ++$i) {
         $roles[] = factory(Role::class)->create(['name' => 'admin ' . $i, 'display_name' => 'Administrator ' . $i]);
     }
     $rolesID = Role::select('*')->orderBy('id', 'desc')->get();
     $rolesDisplayName = Role::select('*')->orderBy('display_name', 'asc')->get();
     $rolesName = Role::select('*')->orderBy('name', 'desc')->get();
     // check order roles with full input
     $res = $this->call('GET', '/roles?sort=name&direction=desc');
     $this->assertEquals(200, $res->getStatusCode());
     $results = json_decode($res->getContent());
     for ($i = 0; $i < count($rolesName); ++$i) {
         $this->assertEquals($rolesName[$i]->id, $results->entities[$i]->id);
     }
     $res = $this->call('GET', '/roles?sort=display_name&direction=asc');
     $this->assertEquals(200, $res->getStatusCode());
     $results = json_decode($res->getContent());
     for ($i = 0; $i < count($results->entities); ++$i) {
         $this->assertEquals($rolesDisplayName[$i]->id, $results->entities[$i]->id);
     }
     // check order roles with equals value of order field, roles is sorted follow id field with desc
     $roles = [];
     for ($i = 0; $i < 10; ++$i) {
         if (in_array($i, [2, 4, 6])) {
             $roles[] = factory(Role::class)->create(['display_name' => 'Administrator']);
         }
         $roles[] = factory(Role::class)->create();
     }
     $roles1 = Role::where('display_name', '=', 'Administrator')->orderBy('id', 'desc')->get();
     $roles2 = Role::where('display_name', '<>', 'Administrator')->orderBy('order', 'asc')->get();
     $roles = array_merge((array) $roles1, (array) $roles2);
     $res = $this->call('GET', '/roles?sort=display_name&direction=asc');
     $this->assertEquals(200, $res->getStatusCode());
     $results = json_decode($res->getContent());
     for ($i = 1; $i < count($roles); ++$i) {
         $this->assertEquals($roles[$i]->id, $results->entities[$i]->id);
     }
 }
コード例 #9
0
 /**
  * index
  * @param  int $id
  * @return json
  */
 public function indexByUser($id)
 {
     $user = \App\User::find($id);
     if (!$user) {
         return response()->json(null, 404);
     }
     $roles = Role::browseByUser(['order' => [Input::get('sort', 'name') => Input::get('direction', 'asc')], 'limit' => $limit = (int) Input::get('limit', 25), 'offset' => (Input::get('page', 1) - 1) * $limit, 'user' => $user]);
     return response()->json(arrayView('phpsoft.users::role/browse', ['roles' => $roles]), 200);
 }