WithPrivileges   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A createPrivilegesForUser() 0 27 5
1
<?php
2
3
namespace Uccello\Core\Support\Traits;
4
5
use App\User;
0 ignored issues
show
Bug introduced by
The type App\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Uccello\Core\Models\Domain;
7
use Uccello\Core\Models\Privilege;
8
use Uccello\Core\Models\Role;
9
10
trait WithPrivileges
11
{
12
    /**
13
     * Creates privileges for user in a domain.
14
     *
15
     * @param \Uccello\Core\Models\Domain $domain
16
     * @param \Uccello\Core\Models\User $user
17
     * @param array $roleIds
18
     * @return array
19
     */
20
    protected function createPrivilegesForUser(Domain $domain, User $user, array $roleIds)
21
    {
22
        $privileges = [ ];
23
24
        foreach ($roleIds as $roleId) {
25
            $role = Role::find($roleId);
26
27
            // Get ancestors domains only if it is allowed
28
            if (config('uccello.roles.display_ancestors_roles')) {
29
                $treeDomainsIds = $domain->findAncestors()->pluck('id');
30
            } else {
31
                $treeDomainsIds = collect([ $domain->id ]);
32
            }
33
34
            if (is_null($role) || !$treeDomainsIds->contains($role->domain->id)) {
35
                continue;
36
            }
37
38
            // Create a new privilege and ignore duplicates
39
            $privileges[ ] = Privilege::firstOrCreate([
40
                'domain_id' => $domain->id,
41
                'role_id' => $role->id,
42
                'user_id' => $user->id
43
            ]);
44
        }
45
46
        return $privileges;
47
    }
48
}