|
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\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Da\User\Contracts\ServiceInterface; |
|
15
|
|
|
use Da\User\Factory\AuthItemFactory; |
|
16
|
|
|
use Da\User\Model\AbstractAuthItem; |
|
17
|
|
|
use Da\User\Traits\AuthManagerAwareTrait; |
|
18
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
|
19
|
|
|
use Exception; |
|
20
|
|
|
|
|
21
|
|
|
class AuthItemEditionService implements ServiceInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use AuthManagerAwareTrait; |
|
24
|
|
|
use ContainerAwareTrait; |
|
25
|
|
|
|
|
26
|
|
|
protected $model; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(AbstractAuthItem $model) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->model = $model; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function run() |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$this->model->validate()) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
try { |
|
39
|
|
|
if ($this->model->getIsNewRecord()) { |
|
40
|
|
|
$item = AuthItemFactory::makeByType($this->model->getType(), $this->model->name); |
|
41
|
|
|
} else { |
|
42
|
|
|
$item = $this->model->item; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$item->name = $this->model->name; |
|
46
|
|
|
$item->description = $this->model->description; |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($this->model->rule)) { |
|
49
|
|
|
if (null !== $this->getAuthManager()->getRule($this->model->rule)) { |
|
50
|
|
|
$item->ruleName = $this->model->rule; |
|
51
|
|
|
} |
|
52
|
|
|
} else { |
|
53
|
|
|
$item->ruleName = null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($this->model->getIsNewRecord()) { |
|
57
|
|
|
$this->getAuthManager()->add($item); |
|
58
|
|
|
} else { |
|
59
|
|
|
$this->getAuthManager()->update($this->model->itemName, $item); |
|
60
|
|
|
$this->model->itemName = $item->name; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->model->item = $item; |
|
64
|
|
|
|
|
65
|
|
|
$this->updateChildren(); |
|
66
|
|
|
} catch (Exception $e) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Updates Auth Item children. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function updateChildren() |
|
79
|
|
|
{ |
|
80
|
|
|
$children = $this->getAuthManager()->getChildren($this->model->item->name); |
|
81
|
|
|
$childrenNames = array_keys($children); |
|
82
|
|
|
|
|
83
|
|
|
if (is_array($this->model->children)) { |
|
84
|
|
|
// remove those not linked anymore |
|
85
|
|
|
foreach (array_diff($childrenNames, $this->model->children) as $item) { |
|
86
|
|
|
if (!$this->getAuthManager()->removeChild($this->model->item, $children[$item])) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
// add new children |
|
91
|
|
|
foreach (array_diff($this->model->children, $childrenNames) as $item) { |
|
92
|
|
|
if (!$this->getAuthManager()->addChild($this->model->item, $this->getAuthManager()->getItem($item))) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} else { |
|
97
|
|
|
return $this->getAuthManager()->removeChildren($this->model->item); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|