Authorization

if you want to authorize model for custom users or condition you can use 2 methods for this action

Method 1

Policies

by default the AtomicModel read the polices for current user and model in non-local environments, register the policy for the model and don`t worry :

namespace App\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class EventPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view the event.
     *
     * @param  \App\User  $user
     * @param  \App\Event  $event
     * @return mixed
     */
    public function view(User $user)
    {
        return true;
        //
    }

    /**
     * Determine whether the user can create events.
     *
     * @param  \App\User  $user
     * @return mixed
     */
    public function create(User $user)
    {
        return false;

    }

    /**
     * Determine whether the user can update the event.
     *
     * @param  \App\User  $user
     * @param  \App\Event  $event
     * @return mixed
     */
    public function update(User $user)
    {
        return false;

    }

    /**
     * Determine whether the user can delete the event.
     *
     * @param  \App\User  $user
     * @param  \App\Event  $event
     * @return mixed
     */
    public function delete(User $user)
    {
        return true;

    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

In AuthServiceProvider.php register the model and policy

/**
 * The policy mappings for the application.
 *
 * @var array
 */
protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy',
    Event::class => EventPolicy::class
];
1
2
3
4
5
6
7
8
9

Method 2

dont want to work with polices ? so you can override Authorization Methods canSee canAdd canDelete canEdit

 /**
  * Current User Can Add New Model
  * @return bool
  */
 public static function canAdd()
 {
     return true;
 }

 /**
  * Current User Can See This Model
  * @return bool
  */
 public static function canSee()
 {
     return true;

 }


 /**
  * Current User Can Delete This Model
  * @return bool
  */
 public static function canDelete()
 {
     return true;


 }

 /**
  * Current User Can Edit This Model
  * @return bool
  */
 public static function canEdit()
 {
     return true;

 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40