Started work on making the public role/user configurable
Create a new 'public' guest user and made the public role visible on role setting screens.
Showing
19 changed files
with
156 additions
and
48 deletions
| ... | @@ -117,7 +117,7 @@ class ChapterController extends Controller | ... | @@ -117,7 +117,7 @@ class ChapterController extends Controller |
| 117 | $this->checkOwnablePermission('chapter-update', $chapter); | 117 | $this->checkOwnablePermission('chapter-update', $chapter); |
| 118 | $chapter->fill($request->all()); | 118 | $chapter->fill($request->all()); |
| 119 | $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id); | 119 | $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id); |
| 120 | - $chapter->updated_by = auth()->user()->id; | 120 | + $chapter->updated_by = user()->id; |
| 121 | $chapter->save(); | 121 | $chapter->save(); |
| 122 | Activity::add($chapter, 'chapter_update', $book->id); | 122 | Activity::add($chapter, 'chapter_update', $book->id); |
| 123 | return redirect($chapter->getUrl()); | 123 | return redirect($chapter->getUrl()); | ... | ... |
| ... | @@ -33,17 +33,16 @@ abstract class Controller extends BaseController | ... | @@ -33,17 +33,16 @@ abstract class Controller extends BaseController |
| 33 | $this->middleware(function ($request, $next) { | 33 | $this->middleware(function ($request, $next) { |
| 34 | 34 | ||
| 35 | // Get a user instance for the current user | 35 | // Get a user instance for the current user |
| 36 | - $user = auth()->user(); | 36 | + $user = user(); |
| 37 | - if (!$user) $user = User::getDefault(); | ||
| 38 | - | ||
| 39 | - // Share variables with views | ||
| 40 | - view()->share('signedIn', auth()->check()); | ||
| 41 | - view()->share('currentUser', $user); | ||
| 42 | 37 | ||
| 43 | // Share variables with controllers | 38 | // Share variables with controllers |
| 44 | $this->currentUser = $user; | 39 | $this->currentUser = $user; |
| 45 | $this->signedIn = auth()->check(); | 40 | $this->signedIn = auth()->check(); |
| 46 | 41 | ||
| 42 | + // Share variables with views | ||
| 43 | + view()->share('signedIn', $this->signedIn); | ||
| 44 | + view()->share('currentUser', $user); | ||
| 45 | + | ||
| 47 | return $next($request); | 46 | return $next($request); |
| 48 | }); | 47 | }); |
| 49 | } | 48 | } | ... | ... |
| ... | @@ -57,7 +57,7 @@ class UserController extends Controller | ... | @@ -57,7 +57,7 @@ class UserController extends Controller |
| 57 | { | 57 | { |
| 58 | $this->checkPermission('users-manage'); | 58 | $this->checkPermission('users-manage'); |
| 59 | $authMethod = config('auth.method'); | 59 | $authMethod = config('auth.method'); |
| 60 | - $roles = $this->userRepo->getAssignableRoles(); | 60 | + $roles = $this->userRepo->getAllRoles(); |
| 61 | return view('users/create', ['authMethod' => $authMethod, 'roles' => $roles]); | 61 | return view('users/create', ['authMethod' => $authMethod, 'roles' => $roles]); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| ... | @@ -126,12 +126,13 @@ class UserController extends Controller | ... | @@ -126,12 +126,13 @@ class UserController extends Controller |
| 126 | return $this->currentUser->id == $id; | 126 | return $this->currentUser->id == $id; |
| 127 | }); | 127 | }); |
| 128 | 128 | ||
| 129 | - $authMethod = config('auth.method'); | ||
| 130 | - | ||
| 131 | $user = $this->user->findOrFail($id); | 129 | $user = $this->user->findOrFail($id); |
| 130 | + | ||
| 131 | + $authMethod = ($user->system_name) ? 'system' : config('auth.method'); | ||
| 132 | + | ||
| 132 | $activeSocialDrivers = $socialAuthService->getActiveDrivers(); | 133 | $activeSocialDrivers = $socialAuthService->getActiveDrivers(); |
| 133 | $this->setPageTitle('User Profile'); | 134 | $this->setPageTitle('User Profile'); |
| 134 | - $roles = $this->userRepo->getAssignableRoles(); | 135 | + $roles = $this->userRepo->getAllRoles(); |
| 135 | return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod, 'roles' => $roles]); | 136 | return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod, 'roles' => $roles]); |
| 136 | } | 137 | } |
| 137 | 138 | ... | ... |
| ... | @@ -132,8 +132,8 @@ class BookRepo extends EntityRepo | ... | @@ -132,8 +132,8 @@ class BookRepo extends EntityRepo |
| 132 | { | 132 | { |
| 133 | $book = $this->book->newInstance($input); | 133 | $book = $this->book->newInstance($input); |
| 134 | $book->slug = $this->findSuitableSlug($book->name); | 134 | $book->slug = $this->findSuitableSlug($book->name); |
| 135 | - $book->created_by = auth()->user()->id; | 135 | + $book->created_by = user()->id; |
| 136 | - $book->updated_by = auth()->user()->id; | 136 | + $book->updated_by = user()->id; |
| 137 | $book->save(); | 137 | $book->save(); |
| 138 | $this->permissionService->buildJointPermissionsForEntity($book); | 138 | $this->permissionService->buildJointPermissionsForEntity($book); |
| 139 | return $book; | 139 | return $book; |
| ... | @@ -149,7 +149,7 @@ class BookRepo extends EntityRepo | ... | @@ -149,7 +149,7 @@ class BookRepo extends EntityRepo |
| 149 | { | 149 | { |
| 150 | $book->fill($input); | 150 | $book->fill($input); |
| 151 | $book->slug = $this->findSuitableSlug($book->name, $book->id); | 151 | $book->slug = $this->findSuitableSlug($book->name, $book->id); |
| 152 | - $book->updated_by = auth()->user()->id; | 152 | + $book->updated_by = user()->id; |
| 153 | $book->save(); | 153 | $book->save(); |
| 154 | $this->permissionService->buildJointPermissionsForEntity($book); | 154 | $this->permissionService->buildJointPermissionsForEntity($book); |
| 155 | return $book; | 155 | return $book; | ... | ... |
| ... | @@ -98,8 +98,8 @@ class ChapterRepo extends EntityRepo | ... | @@ -98,8 +98,8 @@ class ChapterRepo extends EntityRepo |
| 98 | { | 98 | { |
| 99 | $chapter = $this->chapter->newInstance($input); | 99 | $chapter = $this->chapter->newInstance($input); |
| 100 | $chapter->slug = $this->findSuitableSlug($chapter->name, $book->id); | 100 | $chapter->slug = $this->findSuitableSlug($chapter->name, $book->id); |
| 101 | - $chapter->created_by = auth()->user()->id; | 101 | + $chapter->created_by = user()->id; |
| 102 | - $chapter->updated_by = auth()->user()->id; | 102 | + $chapter->updated_by = user()->id; |
| 103 | $chapter = $book->chapters()->save($chapter); | 103 | $chapter = $book->chapters()->save($chapter); |
| 104 | $this->permissionService->buildJointPermissionsForEntity($chapter); | 104 | $this->permissionService->buildJointPermissionsForEntity($chapter); |
| 105 | return $chapter; | 105 | return $chapter; | ... | ... |
| ... | @@ -132,9 +132,8 @@ class EntityRepo | ... | @@ -132,9 +132,8 @@ class EntityRepo |
| 132 | */ | 132 | */ |
| 133 | public function getUserDraftPages($count = 20, $page = 0) | 133 | public function getUserDraftPages($count = 20, $page = 0) |
| 134 | { | 134 | { |
| 135 | - $user = auth()->user(); | ||
| 136 | return $this->page->where('draft', '=', true) | 135 | return $this->page->where('draft', '=', true) |
| 137 | - ->where('created_by', '=', $user->id) | 136 | + ->where('created_by', '=', user()->id) |
| 138 | ->orderBy('updated_at', 'desc') | 137 | ->orderBy('updated_at', 'desc') |
| 139 | ->skip($count * $page)->take($count)->get(); | 138 | ->skip($count * $page)->take($count)->get(); |
| 140 | } | 139 | } | ... | ... |
| ... | @@ -148,8 +148,8 @@ class PageRepo extends EntityRepo | ... | @@ -148,8 +148,8 @@ class PageRepo extends EntityRepo |
| 148 | { | 148 | { |
| 149 | $page = $this->page->newInstance(); | 149 | $page = $this->page->newInstance(); |
| 150 | $page->name = 'New Page'; | 150 | $page->name = 'New Page'; |
| 151 | - $page->created_by = auth()->user()->id; | 151 | + $page->created_by = user()->id; |
| 152 | - $page->updated_by = auth()->user()->id; | 152 | + $page->updated_by = user()->id; |
| 153 | $page->draft = true; | 153 | $page->draft = true; |
| 154 | 154 | ||
| 155 | if ($chapter) $page->chapter_id = $chapter->id; | 155 | if ($chapter) $page->chapter_id = $chapter->id; |
| ... | @@ -330,7 +330,7 @@ class PageRepo extends EntityRepo | ... | @@ -330,7 +330,7 @@ class PageRepo extends EntityRepo |
| 330 | } | 330 | } |
| 331 | 331 | ||
| 332 | // Update with new details | 332 | // Update with new details |
| 333 | - $userId = auth()->user()->id; | 333 | + $userId = user()->id; |
| 334 | $page->fill($input); | 334 | $page->fill($input); |
| 335 | $page->html = $this->formatHtml($input['html']); | 335 | $page->html = $this->formatHtml($input['html']); |
| 336 | $page->text = strip_tags($page->html); | 336 | $page->text = strip_tags($page->html); |
| ... | @@ -363,7 +363,7 @@ class PageRepo extends EntityRepo | ... | @@ -363,7 +363,7 @@ class PageRepo extends EntityRepo |
| 363 | $page->fill($revision->toArray()); | 363 | $page->fill($revision->toArray()); |
| 364 | $page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id); | 364 | $page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id); |
| 365 | $page->text = strip_tags($page->html); | 365 | $page->text = strip_tags($page->html); |
| 366 | - $page->updated_by = auth()->user()->id; | 366 | + $page->updated_by = user()->id; |
| 367 | $page->save(); | 367 | $page->save(); |
| 368 | return $page; | 368 | return $page; |
| 369 | } | 369 | } |
| ... | @@ -381,7 +381,7 @@ class PageRepo extends EntityRepo | ... | @@ -381,7 +381,7 @@ class PageRepo extends EntityRepo |
| 381 | $revision->page_id = $page->id; | 381 | $revision->page_id = $page->id; |
| 382 | $revision->slug = $page->slug; | 382 | $revision->slug = $page->slug; |
| 383 | $revision->book_slug = $page->book->slug; | 383 | $revision->book_slug = $page->book->slug; |
| 384 | - $revision->created_by = auth()->user()->id; | 384 | + $revision->created_by = user()->id; |
| 385 | $revision->created_at = $page->updated_at; | 385 | $revision->created_at = $page->updated_at; |
| 386 | $revision->type = 'version'; | 386 | $revision->type = 'version'; |
| 387 | $revision->summary = $summary; | 387 | $revision->summary = $summary; |
| ... | @@ -404,7 +404,7 @@ class PageRepo extends EntityRepo | ... | @@ -404,7 +404,7 @@ class PageRepo extends EntityRepo |
| 404 | */ | 404 | */ |
| 405 | public function saveUpdateDraft(Page $page, $data = []) | 405 | public function saveUpdateDraft(Page $page, $data = []) |
| 406 | { | 406 | { |
| 407 | - $userId = auth()->user()->id; | 407 | + $userId = user()->id; |
| 408 | $drafts = $this->userUpdateDraftsQuery($page, $userId)->get(); | 408 | $drafts = $this->userUpdateDraftsQuery($page, $userId)->get(); |
| 409 | 409 | ||
| 410 | if ($drafts->count() > 0) { | 410 | if ($drafts->count() > 0) { |
| ... | @@ -535,7 +535,7 @@ class PageRepo extends EntityRepo | ... | @@ -535,7 +535,7 @@ class PageRepo extends EntityRepo |
| 535 | $query = $this->pageRevision->where('type', '=', 'update_draft') | 535 | $query = $this->pageRevision->where('type', '=', 'update_draft') |
| 536 | ->where('page_id', '=', $page->id) | 536 | ->where('page_id', '=', $page->id) |
| 537 | ->where('updated_at', '>', $page->updated_at) | 537 | ->where('updated_at', '>', $page->updated_at) |
| 538 | - ->where('created_by', '!=', auth()->user()->id) | 538 | + ->where('created_by', '!=', user()->id) |
| 539 | ->with('createdBy'); | 539 | ->with('createdBy'); |
| 540 | 540 | ||
| 541 | if ($minRange !== null) { | 541 | if ($minRange !== null) { | ... | ... |
| ... | @@ -35,7 +35,7 @@ class PermissionsRepo | ... | @@ -35,7 +35,7 @@ class PermissionsRepo |
| 35 | */ | 35 | */ |
| 36 | public function getAllRoles() | 36 | public function getAllRoles() |
| 37 | { | 37 | { |
| 38 | - return $this->role->where('hidden', '=', false)->get(); | 38 | + return $this->role->all(); |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | /** | 41 | /** |
| ... | @@ -45,7 +45,7 @@ class PermissionsRepo | ... | @@ -45,7 +45,7 @@ class PermissionsRepo |
| 45 | */ | 45 | */ |
| 46 | public function getAllRolesExcept(Role $role) | 46 | public function getAllRolesExcept(Role $role) |
| 47 | { | 47 | { |
| 48 | - return $this->role->where('id', '!=', $role->id)->where('hidden', '=', false)->get(); | 48 | + return $this->role->where('id', '!=', $role->id)->get(); |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | /** | 51 | /** |
| ... | @@ -90,8 +90,6 @@ class PermissionsRepo | ... | @@ -90,8 +90,6 @@ class PermissionsRepo |
| 90 | { | 90 | { |
| 91 | $role = $this->role->findOrFail($roleId); | 91 | $role = $this->role->findOrFail($roleId); |
| 92 | 92 | ||
| 93 | - if ($role->hidden) throw new PermissionsException("Cannot update a hidden role"); | ||
| 94 | - | ||
| 95 | $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : []; | 93 | $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : []; |
| 96 | $this->assignRolePermissions($role, $permissions); | 94 | $this->assignRolePermissions($role, $permissions); |
| 97 | 95 | ... | ... |
| ... | @@ -199,9 +199,9 @@ class UserRepo | ... | @@ -199,9 +199,9 @@ class UserRepo |
| 199 | * Get the roles in the system that are assignable to a user. | 199 | * Get the roles in the system that are assignable to a user. |
| 200 | * @return mixed | 200 | * @return mixed |
| 201 | */ | 201 | */ |
| 202 | - public function getAssignableRoles() | 202 | + public function getAllRoles() |
| 203 | { | 203 | { |
| 204 | - return $this->role->visible(); | 204 | + return $this->role->all(); |
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | /** | 207 | /** |
| ... | @@ -211,7 +211,7 @@ class UserRepo | ... | @@ -211,7 +211,7 @@ class UserRepo |
| 211 | */ | 211 | */ |
| 212 | public function getRestrictableRoles() | 212 | public function getRestrictableRoles() |
| 213 | { | 213 | { |
| 214 | - return $this->role->where('hidden', '=', false)->where('system_name', '=', '')->get(); | 214 | + return $this->role->where('system_name', '!=', 'admin')->get(); |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | } | 217 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -19,7 +19,7 @@ class ActivityService | ... | @@ -19,7 +19,7 @@ class ActivityService |
| 19 | { | 19 | { |
| 20 | $this->activity = $activity; | 20 | $this->activity = $activity; |
| 21 | $this->permissionService = $permissionService; | 21 | $this->permissionService = $permissionService; |
| 22 | - $this->user = auth()->user(); | 22 | + $this->user = user(); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | /** | 25 | /** | ... | ... |
| ... | @@ -108,8 +108,8 @@ class ImageService | ... | @@ -108,8 +108,8 @@ class ImageService |
| 108 | 'uploaded_to' => $uploadedTo | 108 | 'uploaded_to' => $uploadedTo |
| 109 | ]; | 109 | ]; |
| 110 | 110 | ||
| 111 | - if (auth()->user() && auth()->user()->id !== 0) { | 111 | + if (user()->id !== 0) { |
| 112 | - $userId = auth()->user()->id; | 112 | + $userId = user()->id; |
| 113 | $imageDetails['created_by'] = $userId; | 113 | $imageDetails['created_by'] = $userId; |
| 114 | $imageDetails['updated_by'] = $userId; | 114 | $imageDetails['updated_by'] = $userId; |
| 115 | } | 115 | } | ... | ... |
| ... | @@ -614,7 +614,7 @@ class PermissionService | ... | @@ -614,7 +614,7 @@ class PermissionService |
| 614 | private function currentUser() | 614 | private function currentUser() |
| 615 | { | 615 | { |
| 616 | if ($this->currentUserModel === false) { | 616 | if ($this->currentUserModel === false) { |
| 617 | - $this->currentUserModel = auth()->user() ? auth()->user() : new User(); | 617 | + $this->currentUserModel = user(); |
| 618 | } | 618 | } |
| 619 | 619 | ||
| 620 | return $this->currentUserModel; | 620 | return $this->currentUserModel; | ... | ... |
| ... | @@ -100,7 +100,7 @@ class SocialAuthService | ... | @@ -100,7 +100,7 @@ class SocialAuthService |
| 100 | $socialAccount = $this->socialAccount->where('driver_id', '=', $socialId)->first(); | 100 | $socialAccount = $this->socialAccount->where('driver_id', '=', $socialId)->first(); |
| 101 | $user = $this->userRepo->getByEmail($socialUser->getEmail()); | 101 | $user = $this->userRepo->getByEmail($socialUser->getEmail()); |
| 102 | $isLoggedIn = auth()->check(); | 102 | $isLoggedIn = auth()->check(); |
| 103 | - $currentUser = auth()->user(); | 103 | + $currentUser = user(); |
| 104 | 104 | ||
| 105 | // When a user is not logged in and a matching SocialAccount exists, | 105 | // When a user is not logged in and a matching SocialAccount exists, |
| 106 | // Simply log the user into the application. | 106 | // Simply log the user into the application. |
| ... | @@ -214,9 +214,9 @@ class SocialAuthService | ... | @@ -214,9 +214,9 @@ class SocialAuthService |
| 214 | public function detachSocialAccount($socialDriver) | 214 | public function detachSocialAccount($socialDriver) |
| 215 | { | 215 | { |
| 216 | session(); | 216 | session(); |
| 217 | - auth()->user()->socialAccounts()->where('driver', '=', $socialDriver)->delete(); | 217 | + user()->socialAccounts()->where('driver', '=', $socialDriver)->delete(); |
| 218 | session()->flash('success', title_case($socialDriver) . ' account successfully detached'); | 218 | session()->flash('success', title_case($socialDriver) . ' account successfully detached'); |
| 219 | - return redirect(auth()->user()->getEditUrl()); | 219 | + return redirect(user()->getEditUrl()); |
| 220 | } | 220 | } |
| 221 | 221 | ||
| 222 | } | 222 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -18,7 +18,7 @@ class ViewService | ... | @@ -18,7 +18,7 @@ class ViewService |
| 18 | public function __construct(View $view, PermissionService $permissionService) | 18 | public function __construct(View $view, PermissionService $permissionService) |
| 19 | { | 19 | { |
| 20 | $this->view = $view; | 20 | $this->view = $view; |
| 21 | - $this->user = auth()->user(); | 21 | + $this->user = user(); |
| 22 | $this->permissionService = $permissionService; | 22 | $this->permissionService = $permissionService; |
| 23 | } | 23 | } |
| 24 | 24 | ||
| ... | @@ -84,7 +84,7 @@ class ViewService | ... | @@ -84,7 +84,7 @@ class ViewService |
| 84 | ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type'); | 84 | ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type'); |
| 85 | 85 | ||
| 86 | if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel)); | 86 | if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel)); |
| 87 | - $query = $query->where('user_id', '=', auth()->user()->id); | 87 | + $query = $query->where('user_id', '=', user()->id); |
| 88 | 88 | ||
| 89 | $viewables = $query->with('viewable')->orderBy('updated_at', 'desc') | 89 | $viewables = $query->with('viewable')->orderBy('updated_at', 'desc') |
| 90 | ->skip($count * $page)->take($count)->get()->pluck('viewable'); | 90 | ->skip($count * $page)->take($count)->get()->pluck('viewable'); | ... | ... |
| ... | @@ -5,6 +5,7 @@ use Illuminate\Auth\Authenticatable; | ... | @@ -5,6 +5,7 @@ use Illuminate\Auth\Authenticatable; |
| 5 | use Illuminate\Auth\Passwords\CanResetPassword; | 5 | use Illuminate\Auth\Passwords\CanResetPassword; |
| 6 | use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; | 6 | use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
| 7 | use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; | 7 | use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
| 8 | +use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
| 8 | use Illuminate\Notifications\Notifiable; | 9 | use Illuminate\Notifications\Notifiable; |
| 9 | 10 | ||
| 10 | class User extends Model implements AuthenticatableContract, CanResetPasswordContract | 11 | class User extends Model implements AuthenticatableContract, CanResetPasswordContract |
| ... | @@ -36,21 +37,30 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon | ... | @@ -36,21 +37,30 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon |
| 36 | protected $permissions; | 37 | protected $permissions; |
| 37 | 38 | ||
| 38 | /** | 39 | /** |
| 39 | - * Returns a default guest user. | 40 | + * Returns the default public user. |
| 41 | + * @return User | ||
| 40 | */ | 42 | */ |
| 41 | public static function getDefault() | 43 | public static function getDefault() |
| 42 | { | 44 | { |
| 43 | - return new static([ | 45 | + return static::where('system_name', '=', 'public')->first(); |
| 44 | - 'email' => 'guest', | 46 | + } |
| 45 | - 'name' => 'Guest' | 47 | + |
| 46 | - ]); | 48 | + /** |
| 49 | + * Check if the user is the default public user. | ||
| 50 | + * @return bool | ||
| 51 | + */ | ||
| 52 | + public function isDefault() | ||
| 53 | + { | ||
| 54 | + return $this->system_name === 'public'; | ||
| 47 | } | 55 | } |
| 48 | 56 | ||
| 49 | /** | 57 | /** |
| 50 | * The roles that belong to the user. | 58 | * The roles that belong to the user. |
| 59 | + * @return BelongsToMany | ||
| 51 | */ | 60 | */ |
| 52 | public function roles() | 61 | public function roles() |
| 53 | { | 62 | { |
| 63 | + if ($this->id === 0) return ; | ||
| 54 | return $this->belongsToMany(Role::class); | 64 | return $this->belongsToMany(Role::class); |
| 55 | } | 65 | } |
| 56 | 66 | ... | ... |
| ... | @@ -37,6 +37,16 @@ function versioned_asset($file = '') | ... | @@ -37,6 +37,16 @@ function versioned_asset($file = '') |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | /** | 39 | /** |
| 40 | + * Helper method to get the current User. | ||
| 41 | + * Defaults to public 'Guest' user if not logged in. | ||
| 42 | + * @return \BookStack\User | ||
| 43 | + */ | ||
| 44 | +function user() | ||
| 45 | +{ | ||
| 46 | + return auth()->user() ?: \BookStack\User::getDefault(); | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +/** | ||
| 40 | * Check if the current user has a permission. | 50 | * Check if the current user has a permission. |
| 41 | * If an ownable element is passed in the jointPermissions are checked against | 51 | * If an ownable element is passed in the jointPermissions are checked against |
| 42 | * that particular item. | 52 | * that particular item. |
| ... | @@ -47,7 +57,7 @@ function versioned_asset($file = '') | ... | @@ -47,7 +57,7 @@ function versioned_asset($file = '') |
| 47 | function userCan($permission, Ownable $ownable = null) | 57 | function userCan($permission, Ownable $ownable = null) |
| 48 | { | 58 | { |
| 49 | if ($ownable === null) { | 59 | if ($ownable === null) { |
| 50 | - return auth()->user() && auth()->user()->can($permission); | 60 | + return user() && user()->can($permission); |
| 51 | } | 61 | } |
| 52 | 62 | ||
| 53 | // Check permission on ownable item | 63 | // Check permission on ownable item | ... | ... |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +use Illuminate\Support\Facades\Schema; | ||
| 4 | +use Illuminate\Database\Schema\Blueprint; | ||
| 5 | +use Illuminate\Database\Migrations\Migration; | ||
| 6 | + | ||
| 7 | +class RemoveHiddenRoles extends Migration | ||
| 8 | +{ | ||
| 9 | + /** | ||
| 10 | + * Run the migrations. | ||
| 11 | + * | ||
| 12 | + * @return void | ||
| 13 | + */ | ||
| 14 | + public function up() | ||
| 15 | + { | ||
| 16 | + // Remove the hidden property from roles | ||
| 17 | + Schema::table('roles', function(Blueprint $table) { | ||
| 18 | + $table->dropColumn('hidden'); | ||
| 19 | + }); | ||
| 20 | + | ||
| 21 | + // Add column to mark system users | ||
| 22 | + Schema::table('users', function(Blueprint $table) { | ||
| 23 | + $table->string('system_name')->nullable()->index(); | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + // Insert our new public system user. | ||
| 27 | + $publicUserId = DB::table('users')->insertGetId([ | ||
| 28 | + 'email' => 'guest@example.com', | ||
| 29 | + 'name' => 'Guest', | ||
| 30 | + 'system_name' => 'public', | ||
| 31 | + 'email_confirmed' => true, | ||
| 32 | + 'created_at' => \Carbon\Carbon::now(), | ||
| 33 | + 'updated_at' => \Carbon\Carbon::now(), | ||
| 34 | + ]); | ||
| 35 | + | ||
| 36 | + // Get the public role | ||
| 37 | + $publicRole = DB::table('roles')->where('system_name', '=', 'public')->first(); | ||
| 38 | + | ||
| 39 | + // Connect the new public user to the public role | ||
| 40 | + DB::table('role_user')->insert([ | ||
| 41 | + 'user_id' => $publicUserId, | ||
| 42 | + 'role_id' => $publicRole->id | ||
| 43 | + ]); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Reverse the migrations. | ||
| 48 | + * | ||
| 49 | + * @return void | ||
| 50 | + */ | ||
| 51 | + public function down() | ||
| 52 | + { | ||
| 53 | + Schema::table('roles', function(Blueprint $table) { | ||
| 54 | + $table->boolean('hidden')->default(false); | ||
| 55 | + $table->index('hidden'); | ||
| 56 | + }); | ||
| 57 | + | ||
| 58 | + DB::table('users')->where('system_name', '=', 'public')->delete(); | ||
| 59 | + | ||
| 60 | + Schema::table('users', function(Blueprint $table) { | ||
| 61 | + $table->dropColumn('system_name'); | ||
| 62 | + }); | ||
| 63 | + | ||
| 64 | + DB::table('roles')->where('system_name', '=', 'public')->update(['hidden' => true]); | ||
| 65 | + } | ||
| 66 | +} |
| ... | @@ -79,7 +79,7 @@ | ... | @@ -79,7 +79,7 @@ |
| 79 | <div class="form-group"> | 79 | <div class="form-group"> |
| 80 | <label for="setting-registration-role">{{ trans('settings.reg_default_role') }}</label> | 80 | <label for="setting-registration-role">{{ trans('settings.reg_default_role') }}</label> |
| 81 | <select id="setting-registration-role" name="setting-registration-role" @if($errors->has('setting-registration-role')) class="neg" @endif> | 81 | <select id="setting-registration-role" name="setting-registration-role" @if($errors->has('setting-registration-role')) class="neg" @endif> |
| 82 | - @foreach(\BookStack\Role::visible() as $role) | 82 | + @foreach(\BookStack\Role::all() as $role) |
| 83 | <option value="{{$role->id}}" data-role-name="{{ $role->name }}" | 83 | <option value="{{$role->id}}" data-role-name="{{ $role->name }}" |
| 84 | @if(setting('registration-role', \BookStack\Role::first()->id) == $role->id) selected @endif | 84 | @if(setting('registration-role', \BookStack\Role::first()->id) == $role->id) selected @endif |
| 85 | > | 85 | > | ... | ... |
resources/views/users/forms/system.blade.php
0 → 100644
| 1 | +@if($user->system_name == 'public') | ||
| 2 | + <p>This user represents any guest users that visit your instance. It cannot be used for logins but is assigned automatically.</p> | ||
| 3 | +@endif | ||
| 4 | + | ||
| 5 | +<div class="form-group"> | ||
| 6 | + <label for="name">Name</label> | ||
| 7 | + @include('form.text', ['name' => 'name']) | ||
| 8 | +</div> | ||
| 9 | + | ||
| 10 | +<div class="form-group"> | ||
| 11 | + <label for="email">Email</label> | ||
| 12 | + @include('form.text', ['name' => 'email']) | ||
| 13 | +</div> | ||
| 14 | + | ||
| 15 | +@if(userCan('users-manage')) | ||
| 16 | + <div class="form-group"> | ||
| 17 | + <label for="role">User Role</label> | ||
| 18 | + @include('form/role-checkboxes', ['name' => 'roles', 'roles' => $roles]) | ||
| 19 | + </div> | ||
| 20 | +@endif | ||
| 21 | + | ||
| 22 | +<div class="form-group"> | ||
| 23 | + <a href="{{ baseUrl("/settings/users") }}" class="button muted">Cancel</a> | ||
| 24 | + <button class="button pos" type="submit">Save</button> | ||
| 25 | +</div> |
-
Please register or sign in to post a comment