Guest User

Untitled

a guest
Aug 7th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. class AuthServiceProvider extends ServiceProvider {
  2.  
  3. protected $policies = [
  4. 'AppModel' => 'AppPoliciesModelPolicy',
  5. User::class => UserPolicy::class,
  6. Insured::class => InsuredPolicy::class
  7. ];
  8.  
  9. public function boot(GateContract $gate)
  10. {
  11. $this->registerPolicies($gate);
  12. }
  13. }
  14.  
  15. class UserPolicy {
  16.  
  17. use HandlesAuthorization;
  18.  
  19. protected $user;
  20.  
  21. public function __construct(User $user) {
  22. $this->user = $user;
  23. }
  24.  
  25. public function index(User $user) {
  26. $is_authorized = $user->hasRole('Admin');
  27. return $is_authorized;
  28. }
  29.  
  30. public function show(User $user, User $user_res) {
  31.  
  32. $is_authorized = ($user->id == $user_res->id);
  33. return $is_authorized;
  34. }
  35.  
  36. public function store() {
  37. $is_authorized = $user->hasRole('Admin');
  38. return $is_authorized;
  39. }
  40. }
  41.  
  42. class UserController extends Controller
  43. {
  44.  
  45. public function index()
  46. {
  47. //temporary authentication here
  48. $users = User::all();
  49. $this->authorize('index', User::class);
  50. return $users;
  51. }
  52.  
  53. public function show($id)
  54. {
  55. $user = User::find($id);
  56. $this->authorize('show', $user);
  57. return $user;
  58. }
  59.  
  60. public function store(Request $request) {
  61.  
  62.  
  63. $user = new User;
  64. $user->name = $request->get('name');
  65. $user->email = $request->get('email');
  66. $user->password = Hash::make($request->get('password'));
  67.  
  68. $this->authorize('store', User::class);
  69.  
  70. $user->save();
  71.  
  72. return $user;
  73.  
  74. }
  75. }
  76.  
  77. public function store(User $user) {
  78. $is_authorized = $user->hasRole('Admin');
  79. return true;
  80. }
Add Comment
Please, Sign In to add comment