'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); } }