| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace app\common\library\ps;
- use app\common\library\Auth as FrontendAuth;
- /**
- * 工厂端成员权限。管理员始终全开;操作员按 factory_perms 限制。
- */
- class FactoryPerm
- {
- const ALWAYS = ['factory.view'];
- /**
- * 可分配给操作员的权限(不含管理员专属)
- * @return array
- */
- public static function catalog()
- {
- return [
- ['key' => 'workorder.view', 'label' => '查看工单', 'group' => '工单套打'],
- ['key' => 'workorder.edit', 'label' => '新建 / 编辑工单', 'group' => '工单套打'],
- ['key' => 'workorder.delete', 'label' => '删除工单', 'group' => '工单套打'],
- ['key' => 'workorder.import', 'label' => '文件导入工单', 'group' => '工单套打'],
- ['key' => 'product.view', 'label' => '查看产品', 'group' => '产品工艺'],
- ['key' => 'product.edit', 'label' => '新建 / 编辑产品', 'group' => '产品工艺'],
- ['key' => 'product.delete', 'label' => '删除产品', 'group' => '产品工艺'],
- ['key' => 'product.import', 'label' => '文件导入产品', 'group' => '产品工艺'],
- ['key' => 'template.view', 'label' => '查看模板', 'group' => '本厂模板'],
- ['key' => 'template.edit', 'label' => '修改本厂模板', 'group' => '本厂模板'],
- ['key' => 'template.delete', 'label' => '删除本厂模板', 'group' => '本厂模板'],
- ['key' => 'print.do', 'label' => '套打打印', 'group' => '套打'],
- ['key' => 'print.view', 'label' => '查看打印记录', 'group' => '套打'],
- ['key' => 'print.delete', 'label' => '删除打印记录', 'group' => '套打'],
- ['key' => 'market.view', 'label' => '浏览商城', 'group' => '模板商城'],
- ['key' => 'market.buy', 'label' => '购买模板', 'group' => '模板商城'],
- ];
- }
- /**
- * @return string[]
- */
- public static function keys()
- {
- $keys = [];
- foreach (self::catalog() as $item) {
- $keys[] = $item['key'];
- }
- return $keys;
- }
- /**
- * @param string $key
- * @return string
- */
- public static function label($key)
- {
- foreach (self::catalog() as $item) {
- if ($item['key'] === $key) {
- return $item['label'];
- }
- }
- if ($key === 'factory.view') {
- return '查看本厂信息';
- }
- return $key;
- }
- /**
- * 未单独配置时,操作员默认拥有全部业务权限(与上线前行为一致)
- * @return string[]
- */
- public static function defaults()
- {
- return array_values(array_unique(array_merge(self::ALWAYS, self::keys())));
- }
- /**
- * @param mixed $raw
- * @return string[]|null null 表示沿用默认
- */
- public static function decode($raw)
- {
- if ($raw === null || $raw === '') {
- return null;
- }
- if (is_array($raw)) {
- $list = $raw;
- } else {
- $list = json_decode((string)$raw, true);
- }
- if (!is_array($list)) {
- return null;
- }
- return self::normalize($list, false);
- }
- /**
- * @param array $keys
- * @param bool $keepAlways
- * @return string[]
- */
- public static function normalize(array $keys, $keepAlways = true)
- {
- $allow = array_flip(self::keys());
- $out = [];
- foreach ($keys as $key) {
- $key = trim((string)$key);
- if ($key !== '' && isset($allow[$key])) {
- $out[$key] = $key;
- }
- }
- $implies = [
- 'workorder.edit' => 'workorder.view',
- 'workorder.delete' => 'workorder.view',
- 'workorder.import' => 'workorder.view',
- 'product.edit' => 'product.view',
- 'product.delete' => 'product.view',
- 'product.import' => 'product.view',
- 'template.edit' => 'template.view',
- 'template.delete' => 'template.view',
- 'print.delete' => 'print.view',
- 'market.buy' => 'market.view',
- ];
- foreach ($implies as $from => $to) {
- if (isset($out[$from])) {
- $out[$to] = $to;
- }
- }
- if ($keepAlways) {
- foreach (self::ALWAYS as $key) {
- $out[$key] = $key;
- }
- }
- return array_values($out);
- }
- /**
- * @param mixed $raw
- * @return string[]
- */
- public static function resolve($raw)
- {
- $decoded = self::decode($raw);
- return $decoded === null ? self::defaults() : self::normalize($decoded, true);
- }
- /**
- * @param string $perm
- * @return bool
- */
- public static function can($perm)
- {
- $perm = trim((string)$perm);
- if ($perm === '') {
- return true;
- }
- if (FactoryContext::bypass() || FactoryContext::isApiBound()) {
- return true;
- }
- if (FactoryContext::isOwner()) {
- return true;
- }
- $auth = FrontendAuth::instance();
- if (!$auth || !$auth->isLogin()) {
- return false;
- }
- $raw = isset($auth->factory_perms) ? $auth->factory_perms : '';
- return in_array($perm, self::resolve($raw), true);
- }
- /**
- * @param array $perms
- * @return bool
- */
- public static function canAny(array $perms)
- {
- foreach ($perms as $perm) {
- if (self::can($perm)) {
- return true;
- }
- }
- return false;
- }
- /**
- * @param string $perm
- * @return bool
- */
- public static function deny($perm)
- {
- return !self::can($perm);
- }
- /**
- * 当前登录成员的有效权限
- * @return string[]
- */
- public static function current()
- {
- if (FactoryContext::bypass() || FactoryContext::isApiBound() || FactoryContext::isOwner()) {
- return array_values(array_unique(array_merge(['*:*:*'], self::defaults())));
- }
- $auth = FrontendAuth::instance();
- if (!$auth || !$auth->isLogin()) {
- return self::ALWAYS;
- }
- $raw = isset($auth->factory_perms) ? $auth->factory_perms : '';
- return self::resolve($raw);
- }
- }
|