diff --git a/catch/permissions/model/HasPermissionsTrait.php b/catch/permissions/model/HasPermissionsTrait.php index 006cf1e..64b95ce 100644 --- a/catch/permissions/model/HasPermissionsTrait.php +++ b/catch/permissions/model/HasPermissionsTrait.php @@ -8,3 +8,69 @@ * @copyright By CatchAdmin * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt */ +namespace catchAdmin\permissions\model; + +trait HasPermissionsTrait +{ + /** + * + * @time 2019年12月09日 + * @return \think\model\relation\BelongsToMany + */ + public function permissions(): \think\model\relation\BelongsToMany + { + return $this->belongsToMany(Permissions::class, 'role_has_permissions', 'permission_id', 'role_id'); + } + + /** + * + * @time 2019年12月08日 + * @param array $condition + * @param array $field + * @return mixed + */ + public function getPermissions($condition = [], $field = []) + { + return $this->permissions() + ->when(!empty($field), function ($query) use ($field){ + $query->field($field); + }) + ->when(!empty($condition), function ($query) use ($condition){ + $query->where($condition); + }) + ->select(); + } + + /** + * + * @time 2019年12月08日 + * @param array $permissions + * @return mixed + * @throws \think\db\exception\DbException + */ + public function attach(array $permissions) + { + if (empty($permissions)) { + return true; + } + + sort($permissions); + + return $this->permissions()->attach($permissions); + } + + /** + * + * @time 2019年12月08日 + * @param array $roles + * @return mixed + */ + public function detach(array $roles = []) + { + if (empty($roles)) { + return $this->permissions()->detach(); + } + + return $this->permissions()->detach($roles); + } +}