SecurityRightDao   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 101
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRightsForUser() 0 15 2
A getRightForUser() 0 15 2
A find() 0 4 1
A findOne() 0 4 1
1
<?php
2
3
/*
4
 * This file has been automatically generated by TDBM.
5
 * DO NOT edit this file, as it might be overwritten.
6
 * If you need to perform changes, edit the UserDao class instead!
7
 */
8
9
namespace Mouf\Security\DAO;
10
11
use Kls\Model\Bean\RolesRightBean;
12
use Mouf\Database\TDBM\TDBMService;
13
use Mouf\Database\TDBM\ResultIterator;
14
use Mouf\Security\Rights\RightsRegistry;
15
use Mouf\Security\RightsService\RightInterface;
16
use Mouf\Security\RightsService\RightsDaoInterface;
17
use Mouf\Security\UserService\UserDaoInterface;
18
19
/**
20
 * This class provides a TDBM implementation of the RightsDaoInterface.
21
 */
22
class SecurityRightDao implements RightsDaoInterface
23
{
24
    /**
25
     * @var TDBMService
26
     */
27
    protected $tdbmService;
28
29
    /**
30
     * The list of all supported rights in the application.
31
     *
32
     * @var RightsRegistry
33
     */
34
    protected $rightsRegistry;
35
36
    /**
37
     * @param TDBMService    $tdbmService    Sets the TDBM service used by this DAO.
38
     * @param RightsRegistry $rightsRegistry The list of all supported rights in the application.
39
     */
40
    public function __construct(TDBMService $tdbmService, RightsRegistry $rightsRegistry)
41
    {
42
        $this->tdbmService = $tdbmService;
43
        $this->rightsRegistry = $rightsRegistry;
44
    }
45
46
    /**
47
     * Returns a list of all the rights for the user passed in parameter.
48
     *
49
     * @param string $user_id
50
     *
51
     * @return array<RightInterface>
52
     */
53
    public function getRightsForUser($user_id)
54
    {
55
        $roleRights = $this->find([
56
            'users.id' => $user_id,
57
        ]);
58
59
        $rights = [];
60
61
        foreach ($roleRights as $roleRight) {
62
            $key = $roleRight->getRightKey();
63
            $rights[] = $this->rightsRegistry->get($key);
64
        }
65
66
        return $rights;
67
    }
68
69
    /**
70
     * Returns the RightInterface object associated to the user (or null if the
71
     * user has no such right).
72
     *
73
     * @param string $user_id
74
     * @param string $right
75
     *
76
     * @return RightInterface
77
     */
78
    public function getRightForUser($user_id, $right)
79
    {
80
        $roleRight = $this->findOne([
81
            'users.id' => $user_id,
82
            'right_key' => $right,
83
        ]);
84
85
        if ($roleRight === null) {
86
            return;
87
        }
88
89
        $key = $roleRight->getRightKey();
90
91
        return $this->rightsRegistry->get($key);
92
    }
93
94
    /**
95
     * Get a list of RolesRightBean specified by its filters.
96
     *
97
     * @param mixed $filter                The filter bag (see TDBMService::findObjects for complete description)
98
     * @param array $parameters            The parameters associated with the filter
99
     * @param mixed $orderBy               The order string
100
     * @param array $additionalTablesFetch A list of additional tables to fetch (for performance improvement)
101
     * @param int   $mode                  Either TDBMService::MODE_ARRAY or TDBMService::MODE_CURSOR (for large datasets). Defaults to TDBMService::MODE_ARRAY.
102
     *
103
     * @return RolesRightBean[]|ResultIterator|ResultArray
104
     */
105
    private function find($filter = null, array $parameters = [], $orderBy = null, array $additionalTablesFetch = [], $mode = null)
106
    {
107
        return $this->tdbmService->findObjects('roles_rights', $filter, $parameters, $orderBy, $additionalTablesFetch, $mode);
108
    }
109
110
    /**
111
     * Get a single RolesRightBean specified by its filters.
112
     *
113
     * @param mixed $filter     The filter bag (see TDBMService::findObjects for complete description)
114
     * @param array $parameters The parameters associated with the filter
115
     *
116
     * @return RolesRightBean
117
     */
118
    private function findOne($filter = null, array $parameters = [])
119
    {
120
        return $this->tdbmService->findObject('roles_rights', $filter, $parameters);
121
    }
122
}
123