AuthServiceProvider::boot()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 31
cp 0
rs 7.6764
c 0
b 0
f 0
cc 9
nc 1
nop 1
crap 90
1
<?php
2
3
namespace SET\Providers;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
7
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
8
use Illuminate\Support\Facades\Config;
9
use SET\Note;
10
11
/**
12
 * Class AuthServiceProvider.
13
 */
14
class AuthServiceProvider extends ServiceProvider
15
{
16
    /**
17
     * The policy mappings for the application.
18
     *
19
     * @var array
20
     */
21
    protected $policies = [
22
        'SET\Model' => 'SET\Policies\ModelPolicy',
23
    ];
24
25
    /**
26
     * @param GateContract $gate
27
     */
28
    public function boot(GateContract $gate)
29
    {
30
        $this->registerPolicies($gate);
31
32
        //user can edit this. AKA, they are an admin.
33
        $gate->define('edit', function ($user) {
34
            return $this->isAdmin($user);
35
        });
36
37
        //user has view rights
38
        $gate->define('view', function ($user) {
39
            return $this->isViewer($user);
40
        });
41
42
        //Let admin update note & user update training note.
43
        $gate->define('update_record', function ($user, $record) {
44
            return $user->id == $record->user_id || $this->isAdmin($user);
45
        });
46
47
        // primarily used to set javascript variable.
48
        $gate->define('update_self', function ($user, $page) {
49
            return $user->id == $page->id && !$this->isAdmin($user);
50
        });
51
52
        $gate->define('show_user', function ($user, $page) {
53
            return $this->isViewer($user) || $user->id === $page->id;
54
        });
55
56
        $gate->define('edit_training_user', function ($user, $page) {
57
            return $this->isAdmin($user) || $user->id === $page->id;
58
        });
59
60
        $gate->define('show_note', function ($user, $page) {
61
            return $this->isAdmin($user) || Note::findOrFail($page->id)->user()->id === $user->id;
62
        });
63
64
        $gate->define('show_published_news', function ($user, $news) {
65
            return $this->isViewer($user) ||
66
                    ($news->publish_date <= Carbon::today() &&
67
                      ($news->expire_date >= Carbon::today() ||
68
                        is_null($news->expire_date)));
69
        });
70
    }
71
72
    /**
73
     * Register the application services.
74
     *
75
     * @return void
76
     */
77
    public function register()
78
    {
79
        //
80
    }
81
82
    /**
83
     * Check if we have defined the user as an admin in their record on in the config file.
84
     *
85
     * @param $user
86
     *
87
     * @return bool
88
     */
89
    private function isAdmin($user)
90
    {
91
        return $user->role == 'edit' || in_array($user->username, Config::get('auth.admin'));
92
    }
93
94
    /**
95
     * See if we have set the role to view or higher.
96
     *
97
     * @param $user
98
     *
99
     * @return bool
100
     */
101
    private function isViewer($user)
102
    {
103
        return $user->role == 'view' || $this->isAdmin($user);
104
    }
105
}
106