1 | <?php declare(strict_types=1); |
||
25 | class Controller extends \samsoncms\Application |
||
26 | { |
||
27 | /** Application access right name pattern */ |
||
28 | const RIGHT_APPLICATION_KEY = '/^APPLICATION_(?<application>.*)/ui'; |
||
29 | |||
30 | /** @var array User group rights cache */ |
||
31 | protected $rightsCache = array(); |
||
32 | |||
33 | /** Application name */ |
||
34 | public $name = 'Права'; |
||
35 | |||
36 | /** Application description */ |
||
37 | public $description = 'Права доступа'; |
||
38 | |||
39 | /** Application icon*/ |
||
40 | public $icon = 'unlock'; |
||
41 | |||
42 | /** Identifier */ |
||
43 | public $id = 'security'; |
||
44 | |||
45 | public $dbGroupIdField = 'group_id'; |
||
46 | |||
47 | /** @var string Module identifier */ |
||
48 | protected $entity = '\samson\activerecord\group'; |
||
49 | |||
50 | /** @var string SamsonCMS application form class */ |
||
51 | protected $formClassName = '\samsoncms\app\security\Form'; |
||
52 | |||
53 | /** @var QueryInterface Database query instance */ |
||
54 | protected $query; |
||
55 | |||
56 | /** |
||
57 | * @var \samsonframework\i18n\i18nInterface |
||
58 | * @InjectClass("samsonframework\i18n\I18nInterface") |
||
59 | */ |
||
60 | protected $i18n; |
||
61 | |||
62 | //[PHPCOMPRESSOR(remove,start)] |
||
63 | public function prepare() |
||
83 | //[PHPCOMPRESSOR(remove,end)] |
||
84 | |||
85 | /** |
||
86 | * Asynchronous change group right controller action |
||
87 | * @param string $groupID Group identifier |
||
88 | * @param string $rightID Right identifier |
||
89 | * @return array Asynchronous response array |
||
90 | */ |
||
91 | public function __async_change($groupID, $rightID) |
||
118 | |||
119 | /** |
||
120 | * Core routing(core.routing) event handler |
||
121 | * @param \samson\core\Core $core |
||
122 | * @param boolean $securityResult |
||
123 | * @return boolean True if security passed |
||
124 | */ |
||
125 | public function handle(&$core, &$securityResult) |
||
126 | { |
||
127 | // Remove URL base from current URL, split by '/' |
||
128 | $parts = explode(__SAMSON_BASE__, $_SERVER['REQUEST_URI']); |
||
129 | $parts = array_values(array_filter($parts)); |
||
130 | $cmsUrl = isset($parts[0]) ? $parts[0] : ''; |
||
131 | |||
132 | if ($cmsUrl == $core->module('cms')->baseUrl) { |
||
133 | // Get module identifier |
||
134 | $module = isset($parts[1]) ? $parts[1] : ''; |
||
135 | |||
136 | // Get action identifier |
||
137 | //$action = isset($parts[1]) ? $parts[1] : ''; |
||
138 | // Get parameter values collection |
||
139 | //$params = sizeof($parts) > 2 ? array_slice($parts, 2) : array(); |
||
140 | $social = & $this->system->module('social'); |
||
141 | |||
142 | // If we have are authorized |
||
143 | if ($social->authorized()) { |
||
144 | /**@var \samson\activerecord\user Get authorized user object */ |
||
145 | $authorizedUser = $social->user(); |
||
146 | |||
147 | $dbTable = $social->dbTable; |
||
148 | $groupIdField = $dbTable::$_attributes[$this->dbGroupIdField]; |
||
149 | |||
150 | // Try to load security group rights from cache |
||
151 | $userRights = & $this->rightsCache[$authorizedUser->$groupIdField]; |
||
152 | if (!isset($userRights)) { |
||
153 | // Parse security group rights and store it to cache |
||
154 | $userRights = $this->parseGroupRights($authorizedUser->$groupIdField); |
||
155 | } |
||
156 | |||
157 | // Hide all applications except with access rights |
||
158 | foreach (self::$loaded as $application) { |
||
159 | if (in_array($application->id, $userRights['application']) |
||
160 | // && !in_array(Right::APPLICATION_ACCESS_ALL, $userRights['application']) |
||
161 | && $authorizedUser->$groupIdField != 1 |
||
162 | ) { |
||
163 | $application->hide = true; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | // If we have full right to access all applications or admin |
||
168 | if (in_array(Right::APPLICATION_ACCESS_ALL, $userRights['application']) || $authorizedUser->$groupIdField == 1) { |
||
169 | return $securityResult = true; |
||
170 | } else if (in_array($module, $userRights['application'])) { // Try to find right to access current application |
||
171 | return $securityResult = true; |
||
172 | } else if ($module == '' && in_array('template', $userRights['application'])) {// Main page(empty url) |
||
173 | return $securityResult = true; |
||
174 | } else { // We cannot access this application |
||
175 | return $securityResult = false; |
||
176 | } |
||
177 | } |
||
178 | } else { |
||
179 | return $securityResult = true; |
||
180 | } |
||
181 | |||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Parse application access right |
||
186 | * @param string $rightName Right name |
||
187 | * @return string Application name |
||
188 | */ |
||
189 | private function matchApplicationAccessRight($rightName, &$applicationName) |
||
201 | |||
202 | /** |
||
203 | * Clear all database security rights records that do not match current application list |
||
204 | * @param array $accessibleApplications Collection of loaded applications |
||
205 | */ |
||
206 | private function clearUnmatchedRights(array $accessibleApplications) |
||
220 | |||
221 | /** |
||
222 | * Parse database application user group rights |
||
223 | * @param integer $groupID Security group identifier |
||
224 | * @return array Parsed user group rights |
||
225 | */ |
||
226 | public function parseGroupRights($groupID) |
||
252 | |||
253 | /** Application initialization */ |
||
254 | public function init(array $params = array()) |
||
289 | } |
||
290 |