Dan Brown

Added a whole load of permission & role tests

...@@ -15,10 +15,10 @@ class Activity extends Model ...@@ -15,10 +15,10 @@ class Activity extends Model
15 15
16 /** 16 /**
17 * Get the entity for this activity. 17 * Get the entity for this activity.
18 - * @return bool
19 */ 18 */
20 public function entity() 19 public function entity()
21 { 20 {
21 + if ($this->entity_type === '') $this->entity_type = null;
22 return $this->morphTo('entity'); 22 return $this->morphTo('entity');
23 } 23 }
24 24
......
...@@ -35,6 +35,7 @@ class UserController extends Controller ...@@ -35,6 +35,7 @@ class UserController extends Controller
35 */ 35 */
36 public function index() 36 public function index()
37 { 37 {
38 + $this->checkPermission('users-manage');
38 $users = $this->userRepo->getAllUsers(); 39 $users = $this->userRepo->getAllUsers();
39 $this->setPageTitle('Users'); 40 $this->setPageTitle('Users');
40 return view('users/index', ['users' => $users]); 41 return view('users/index', ['users' => $users]);
......
...@@ -136,7 +136,7 @@ class BookRepo ...@@ -136,7 +136,7 @@ class BookRepo
136 */ 136 */
137 public function newFromInput($input) 137 public function newFromInput($input)
138 { 138 {
139 - return $this->bookQuery()->fill($input); 139 + return $this->book->newInstance($input);
140 } 140 }
141 141
142 /** 142 /**
......
...@@ -101,6 +101,7 @@ class PermissionsRepo ...@@ -101,6 +101,7 @@ class PermissionsRepo
101 public function assignRolePermissions(Role $role, $permissionNameArray = []) 101 public function assignRolePermissions(Role $role, $permissionNameArray = [])
102 { 102 {
103 $permissions = []; 103 $permissions = [];
104 + $permissionNameArray = array_values($permissionNameArray);
104 if ($permissionNameArray && count($permissionNameArray) > 0) { 105 if ($permissionNameArray && count($permissionNameArray) > 0) {
105 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray(); 106 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
106 } 107 }
......
...@@ -67,11 +67,12 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon ...@@ -67,11 +67,12 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
67 67
68 /** 68 /**
69 * Get all permissions belonging to a the current user. 69 * Get all permissions belonging to a the current user.
70 + * @param bool $cache
70 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough 71 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
71 */ 72 */
72 - public function permissions() 73 + public function permissions($cache = true)
73 { 74 {
74 - if(isset($this->permissions)) return $this->permissions; 75 + if(isset($this->permissions) && $cache) return $this->permissions;
75 $this->load('roles.permissions'); 76 $this->load('roles.permissions');
76 $permissions = $this->roles->map(function($role) { 77 $permissions = $this->roles->map(function($role) {
77 return $role->permissions; 78 return $role->permissions;
...@@ -106,7 +107,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon ...@@ -106,7 +107,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
106 */ 107 */
107 public function attachRoleId($id) 108 public function attachRoleId($id)
108 { 109 {
109 - $this->roles()->attach([$id]); 110 + $this->roles()->attach($id);
110 } 111 }
111 112
112 /** 113 /**
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 16
17 {{ $activity->getText() }} 17 {{ $activity->getText() }}
18 18
19 - @if($activity->entity()) 19 + @if($activity->entity)
20 <a href="{{ $activity->entity->getUrl() }}">{{ $activity->entity->name }}</a> 20 <a href="{{ $activity->entity->getUrl() }}">{{ $activity->entity->name }}</a>
21 @endif 21 @endif
22 22
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 <label> @include('settings/roles/checkbox', ['permission' => 'users-manage']) Manage users</label> 17 <label> @include('settings/roles/checkbox', ['permission' => 'users-manage']) Manage users</label>
18 </div> 18 </div>
19 <div class="col-md-6"> 19 <div class="col-md-6">
20 - <label>@include('settings/roles/checkbox', ['permission' => 'user-roles-manage']) Manage user roles & Permissions</label> 20 + <label>@include('settings/roles/checkbox', ['permission' => 'user-roles-manage']) Manage user roles</label>
21 </div> 21 </div>
22 </div> 22 </div>
23 <hr class="even"> 23 <hr class="even">
......
...@@ -85,6 +85,17 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase ...@@ -85,6 +85,17 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
85 } 85 }
86 86
87 /** 87 /**
88 + * Quick way to create a new user without any permissions
89 + * @param array $attributes
90 + * @return mixed
91 + */
92 + protected function getNewBlankUser($attributes = [])
93 + {
94 + $user = factory(\BookStack\User::class)->create($attributes);
95 + return $user;
96 + }
97 +
98 + /**
88 * Assert that a given string is seen inside an element. 99 * Assert that a given string is seen inside an element.
89 * 100 *
90 * @param bool|string|null $element 101 * @param bool|string|null $element
......