public function down() { $userTable = Configs::instance()->userTable; if ($this->db->schema->getTableSchema($userTable, true) !== null) { $this->dropTable($userTable); } }
/** * @inheritdoc */ public static function getDb() { if (Configs::instance()->db !== null) { return Configs::instance()->db; } else { return parent::getDb(); } }
/** * @inheritdoc */ public function down() { $this->dropTable(Configs::instance()->menuTable); }
/** * Check access route for user. * @param string|array $route * @param integer|User $user * @return boolean */ public static function checkRoute($route, $params = [], $user = null) { $config = Configs::instance(); $r = static::normalizeRoute($route); if ($config->onlyRegisteredRoute && !isset(static::getRegisteredRoutes()[$r])) { return true; } if ($user === null) { $user = Yii::$app->getUser(); } $userId = $user instanceof User ? $user->getId() : $user; if ($config->strict) { if ($user->can($r, $params)) { return true; } while (($pos = strrpos($r, '/')) > 0) { $r = substr($r, 0, $pos); if ($user->can($r . '/*', $params)) { return true; } } return $user->can('/*', $params); } else { $routes = static::getRoutesByUser($userId); if (isset($routes[$r])) { return true; } while (($pos = strrpos($r, '/')) > 0) { $r = substr($r, 0, $pos); if (isset($routes[$r . '/*'])) { return true; } } return isset($routes['/*']); } }
/** * Use to get assigned menu of user. * @param mixed $userId * @param integer $root * @param \Closure $callback use to reformat output. * callback should have format like * * ``` * function ($menu) { * return [ * 'label' => $menu['name'], * 'url' => [$menu['route']], * 'options' => $data, * 'items' => $menu['children'] * ] * ] * } * ``` * @param boolean $refresh * @return array */ public static function getAssignedMenu($userId, $root = null, $callback = null, $refresh = false) { $config = Configs::instance(); /* @var $manager \yii\rbac\BaseManager */ $manager = Yii::$app->getAuthManager(); $menus = Menu::find()->asArray()->indexBy('id')->all(); $key = [__METHOD__, $userId, $manager->defaultRoles]; $cache = $config->cache; if ($refresh || $cache === null || ($assigned = $cache->get($key)) === false) { $routes = $filter1 = $filter2 = []; if ($userId !== null) { foreach ($manager->getPermissionsByUser($userId) as $name => $value) { if ($name[0] === '/') { if (substr($name, -2) === '/*') { $name = substr($name, 0, -1); } $routes[] = $name; } } } foreach ($manager->defaultRoles as $role) { foreach ($manager->getPermissionsByRole($role) as $name => $value) { if ($name[0] === '/') { if (substr($name, -2) === '/*') { $name = substr($name, 0, -1); } $routes[] = $name; } } } $routes = array_unique($routes); sort($routes); $prefix = '\\'; foreach ($routes as $route) { if (strpos($route, $prefix) !== 0) { if (substr($route, -1) === '/') { $prefix = $route; $filter1[] = $route . '%'; } else { $filter2[] = $route; } } } $assigned = []; $query = Menu::find()->select(['id'])->asArray(); if (count($filter2)) { $assigned = $query->where(['route' => $filter2])->column(); } if (count($filter1)) { $query->where('route like :filter'); foreach ($filter1 as $filter) { $assigned = array_merge($assigned, $query->params([':filter' => $filter])->column()); } } $assigned = static::requiredParent($assigned, $menus); if ($cache !== null) { $cache->set($key, $assigned, $config->cacheDuration, new TagDependency(['tags' => Configs::CACHE_TAG])); } } $key = [__METHOD__, $assigned, $root]; if ($refresh || $callback !== null || $cache === null || ($result = $cache->get($key)) === false) { $result = static::normalizeMenu($assigned, $menus, $callback, $root); if ($cache !== null && $callback === null) { $cache->set($key, $result, $config->cacheDuration, new TagDependency(['tags' => Configs::CACHE_TAG])); } } return $result; }
/** * Get list of application routes * @return array */ public function getAppRoutes($module = null) { if ($module === null) { $module = Yii::$app; } elseif (is_string($module)) { $module = Yii::$app->getModule($module); } $key = [__METHOD__, $module->getUniqueId()]; $cache = Configs::instance()->cache; if ($cache === null || ($result = $cache->get($key)) === false) { $result = []; $this->getRouteRecrusive($module, $result); if ($cache !== null) { $cache->set($key, $result, Configs::instance()->cacheDuration, new TagDependency(['tags' => self::CACHE_TAG])); } } return $result; }
/** * @inheritdoc */ public static function tableName() { return Configs::instance()->userTable; }
/** * Get avalible menu. * @return array */ public function getMenus() { if ($this->_normalizeMenus === null) { $mid = '/' . $this->getUniqueId() . '/'; // resolve core menus $this->_normalizeMenus = []; $config = components\Configs::instance(); $conditions = ['user' => $config->db && $config->db->schema->getTableSchema($config->userTable), 'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\\db\\BaseActiveRecord'), 'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable)]; foreach ($this->_coreItems as $id => $lable) { if (!isset($conditions[$id]) || $conditions[$id]) { $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]]; } } foreach (array_keys($this->controllerMap) as $id) { $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]]; } // user configure menus foreach ($this->_menus as $id => $value) { if (empty($value)) { unset($this->_normalizeMenus[$id]); continue; } if (is_string($value)) { $value = ['label' => $value]; } $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value) : $value; if (!isset($this->_normalizeMenus[$id]['url'])) { $this->_normalizeMenus[$id]['url'] = [$mid . $id]; } } } return $this->_normalizeMenus; }