1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Component; |
13
|
|
|
|
14
|
|
|
use Da\User\Contracts\AuthManagerInterface; |
15
|
|
|
use yii\db\Query; |
16
|
|
|
use yii\rbac\DbManager; |
17
|
|
|
|
18
|
|
|
class AuthDbManagerComponent extends DbManager implements AuthManagerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param int|null $type If null will return all auth items |
22
|
|
|
* @param array $excludeItems Items that should be excluded from result array |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function getItems($type = null, $excludeItems = []) |
27
|
|
|
{ |
28
|
|
|
$query = (new Query())->from($this->itemTable); |
29
|
|
|
|
30
|
|
|
if ($type !== null) { |
31
|
|
|
$query->where(['type' => $type]); |
32
|
|
|
} else { |
33
|
|
|
$query->orderBy('type'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
foreach ($excludeItems as $name) { |
37
|
|
|
$query->andWhere('name <> :item', ['item' => $name]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$items = []; |
41
|
|
|
|
42
|
|
|
foreach ($query->all($this->db) as $row) { |
43
|
|
|
$items[$row['name']] = $this->populateItem($row); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $items; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns both roles and permissions assigned to user. |
51
|
|
|
* |
52
|
|
|
* @param int $userId |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getItemsByUser($userId) |
57
|
|
|
{ |
58
|
|
|
if (empty($userId)) { |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$query = (new Query()) |
63
|
|
|
->select('b.*') |
64
|
|
|
->from(['a' => $this->assignmentTable, 'b' => $this->itemTable]) |
65
|
|
|
->where('{{a}}.[[item_name]]={{b}}.[[name]]') |
66
|
|
|
->andWhere(['a.user_id' => (string)$userId]); |
67
|
|
|
|
68
|
|
|
$roles = []; |
69
|
|
|
foreach ($query->all($this->db) as $row) { |
70
|
|
|
$roles[$row['name']] = $this->populateItem($row); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $roles; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function getItem($name) |
80
|
|
|
{ |
81
|
|
|
return parent::getItem($name); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|