public function setUp() { parent::setUp(); \Birdmin\Product::where('name', 'Test Product')->delete(); Session::start(); $this->be(User::find(1)); }
/** * Installs the basic environment. * Includes super user and basic role assignment. * * @return void */ public function run() { $user = User::create(['first_name' => 'Mike', 'last_name' => 'Adamczyk', 'email' => '*****@*****.**', 'password' => Hash::make('password'), 'position' => 'Web Developer', 'affiliation' => 'Brightstar Corporation', 'website' => 'http://bom.us']); // Sample user. $admin = User::create(['first_name' => 'Robin', 'last_name' => 'Bird', 'email' => '*****@*****.**', 'password' => Hash::make('password')]); $roles = [['name' => 'Super User', 'description' => 'Provides full access to the application.'], ['name' => 'Administrator', 'description' => 'Provides non-system content and object management.'], ['name' => 'Editor', 'description' => 'Provides non-system content-management access.']]; foreach ($roles as $data) { $role = Role::create($data); } // Assign the role to the admin user. $user->assignRole('Super User'); $admin->assignRole('Administrator'); }
/** * Test basic user authorizations, with models. * This should deal directly with the ModelPolicy class. */ public function test_basic_user_auth() { // User 2 has permission to do a couple things. $user = User::find(2); $this->assertTrue($user->hasRole(Role::getByName('Administrator'))); // The models we'll test. $page = Page::find(1); $this->assertTrue($user->can('view', $page)); $this->assertFalse($user->can('delete', $page)); // User model is a managed class. The user doesn't have the manage permission. // So, They shouldn't be able to edit a user that doesn't belong to them. $testUser = User::find(1); $this->assertFalse($user->can('edit', $testUser)); // But they can edit themselves. $this->assertTrue($user->can('edit', $user)); }
/** * Give this role to a user. * @param User $user * @return null|Collection */ public function assign(User $user) { if ($user->hasRole($this)) { return null; } return $user->roles()->attach($this); }
/** * Can manage other objects. * @param User $user * @param Model $model * @return bool */ public function manage(User $user, $model, $ability = null) { // If the user has the manage permission, let them do whatever. if ($user->permissions()->exists('manage', $model)) { return true; } // if the model is just the class and the ability is just to view. // We don't have an object to check. if ($ability == 'view') { return $this->view($user, $model); } // We already found out that the user doesn't have the manage permission for this class. if (is_string($model)) { return false; } // Otherwise, check the id of the object against the owner's user id. return $user->id === $model->ownerId(); }
/** * Reverse the migrations. * * @return void */ public function down() { User::blueprint()->dropSchema(); }