| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace services\rbac;
- use backend\models\AdminRole;
- use Yii;
- use common\components\Service;
- use common\enums\StatusEnum;
- use common\enums\WhetherEnum;
- use common\helpers\ArrayHelper;
- use common\helpers\TreeHelper;
- use common\models\rbac\AuthRole;
- use common\enums\AppEnum;
- use yii\web\UnauthorizedHttpException;
- /**
- *
- * 角色
- *
- * Class AuthRoleService
- * @package services\rbac
- * @author qimall
- */
- class AuthRoleService extends Service
- {
- /**
- * 角色信息
- *
- * @var array
- */
- protected $roles = [];
- /**
- * 获取当前角色信息
- *
- * @return array|\yii\db\ActiveRecord|null
- * @throws UnauthorizedHttpException
- */
- public function getRole()
- {
- if (Yii::$app->services->auth->isSuperAdmin()) {
- return [];
- }
- if(empty(Yii::$app->user->identity) || empty(Yii::$app->user->identity->getId())) return [];
- $admin_role = AdminRole::findOne(['admin_id'=>Yii::$app->user->identity->getId()]);
- if(empty($admin_role)){
- return [];
- }
- return $admin_role;
- }
- /**
- * 检测用户商场管理员角色
- * @return bool
- */
- public function isMallAdmin()
- {
- $admin_role = AdminRole::findOne(['admin_id'=>Yii::$app->user->identity->getId()]);
- if (empty($admin_role)) return false;
- $auth = AuthRole::findOne(['id' => $admin_role->role_id, 'is_default' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED]);
- if ($auth) return true;
- return false;
- }
- /**
- * 获取编辑的数据
- *
- * @param int $role_id
- * @param array $allAuth
- * @return array
- *
- */
- public function getJsTreeData($role_id, array $allAuth)
- {
- // 当前角色已有的权限
- $auth = Yii::$app->services->rbacAuthItemChild->findItemByRoleId($role_id);
- $addonName = $formAuth = $checkIds = $addonFormAuth = $addonsCheckIds = [];
- // 区分默认和插件权限
- foreach ($allAuth as $item) {
- if ($item['is_addon'] == WhetherEnum::DISABLED) {
- $formAuth[] = $item;
- } else {
- if ($item['pid'] == 0) {
- $item['pid'] = $item['addons_name'];
- }
- $addonFormAuth[] = $item;
- $addonName[$item['addons_name']] = $item['addons_name'];
- }
- }
- // 获取顶级插件数据
- $addons = Yii::$app->services->addons->findByNames(array_keys($addonName));
- foreach ($addons as $addon) {
- $addonFormAuth[] = [
- 'id' => $addon['name'],
- 'pid' => 0,
- 'title' => $addon['title'],
- ];
- }
- // 区分默认和插件权限ID
- foreach ($auth as $value) {
- if ($value['is_addon'] == WhetherEnum::DISABLED) {
- $checkIds[] = $value['id'];
- } else {
- $addonsCheckIds[] = $value['id'];
- }
- }
- return [$formAuth, $checkIds, $addonFormAuth, $addonsCheckIds];
- }
- /**
- * 获取角色名称
- *
- * @return array|mixed|string
- * @throws UnauthorizedHttpException
- */
- public function getTitle()
- {
- return $this->getRole()['title'] ?? '游客';
- }
- /**
- * 获取上级角色
- *
- * 注意:如果是其他应用则需要自行获取上级
- *
- * @param $id
- * @return array
- */
- public function getDropDownForEdit($app_id, $id = '')
- {
- $list = $this->findAll($app_id, Yii::$app->services->merchant->getId());
- $list = ArrayHelper::removeByValue($list, $id);
- $models = ArrayHelper::itemsMerge($list);
- $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
- if (Yii::$app->services->auth->isSuperAdmin()) {
- return ArrayHelper::merge([0 => '顶级角色'], $data);
- }
- return $data;
- }
- /**
- * 获取是否所有角色的条件
- *
- * @param bool $sourceAuthChild
- * @return array
- * @throws UnauthorizedHttpException
- */
- public function roleCondition($sourceAuthChild = false)
- {
- if ($sourceAuthChild == false) {
- return [];
- }
- $role = Yii::$app->services->rbacAuthRole->getRole();
- if (empty($role)) {
- return [];
- }
- $tree = $role['tree'] . TreeHelper::prefixTreeKey($role['id']);
- return ['like', 'tree', $tree . '%', false];
- }
- /**
- * 获取分配角色列表
- *
- * @param string $app_id 应用id
- * @param bool $sourceAuthChild 权限来源(false:所有权限,true:当前角色)
- * @return array
- * @throws UnauthorizedHttpException
- */
- public function getDropDown($app_id, $sourceAuthChild = false)
- {
- $list = $this->findAll($app_id, Yii::$app->services->merchant->getId(), $this->roleCondition($sourceAuthChild));
- $pid = 0;
- $treeStat = 1;
- if ($sourceAuthChild == true && ($role = Yii::$app->services->rbacAuthRole->getRole())) {
- $pid = $role['id'];
- $treeStat = $role['level'] + 1;
- }
- $models = ArrayHelper::itemsMerge($list, $pid);
- return ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models, 'id', 'title', $treeStat), 'id', 'title');
- }
- /**
- * 获取当前角色的子角色
- *
- * @return array
- * @throws UnauthorizedHttpException
- */
- public function getChildes($app_id): array
- {
- return $this->findAll($app_id, Yii::$app->services->merchant->getId(), $this->roleCondition(true));
- }
- /**
- * 复制默认角色进入商户
- *
- * @param $app_id
- * @param $merchant_id
- * @return bool|AuthRole
- * @throws \yii\db\Exception
- */
- public function cloneInDefault($app_id, $merchant_id)
- {
- if (!($default = $this->findDefault($app_id))) {
- return false;
- }
- $role = new AuthRole();
- $role->attributes = $default;
- $role->merchant_id = $merchant_id;
- if ($role->save()) {
- Yii::$app->services->rbacAuthItemChild->accreditByDefault($role, $default['authItemChild']);
- return $role;
- }
- return false;
- }
- /**
- * 查询所有角色信息
- *
- * @return array
- */
- public function findAll($app_id, $merchant_id, $condition = []): array
- {
- return AuthRole::find()
- ->where(['app_id' => $app_id])
- ->andWhere(['>=', 'status', StatusEnum::DISABLED])
- ->andFilterWhere(['merchant_id' => $merchant_id])
- ->andFilterWhere($condition)
- ->orderBy('sort asc, created_at asc')
- ->asArray()
- ->all();
- }
- /**
- * 获取默认已启用的角色
- *
- * @param $app_id
- * @return array|\yii\db\ActiveRecord|null
- */
- public function findDefault($app_id)
- {
- return AuthRole::find()
- ->where([
- 'is_default' => StatusEnum::ENABLED,
- 'app_id' => $app_id,
- 'status' => StatusEnum::ENABLED,
- ])
- ->with('authItemChild')
- ->asArray()
- ->one();
- }
- /**
- * 获取当前商户默认已启用的角色
- *
- * @param $app_id
- * @return array|\yii\db\ActiveRecord|null|AuthRole
- */
- public function findDefaultByMerchantId($merchant_id, $app_id = AppEnum::MERCHANT)
- {
- return AuthRole::find()
- ->where([
- 'is_default' => StatusEnum::ENABLED,
- 'app_id' => $app_id,
- 'merchant_id' => $merchant_id,
- 'status' => StatusEnum::ENABLED,
- ])
- ->one();
- }
- /**
- * @param $id
- * @return array|null|\yii\db\ActiveRecord
- */
- public function findById($id)
- {
- return AuthRole::find()
- ->where(['id' => $id])
- ->asArray()
- ->one();
- }
- }
|