FactoryPerm.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\common\library\ps;
  3. use app\common\library\Auth as FrontendAuth;
  4. /**
  5. * 工厂端成员权限。管理员始终全开;操作员按 factory_perms 限制。
  6. */
  7. class FactoryPerm
  8. {
  9. const ALWAYS = ['factory.view'];
  10. /**
  11. * 可分配给操作员的权限(不含管理员专属)
  12. * @return array
  13. */
  14. public static function catalog()
  15. {
  16. return [
  17. ['key' => 'workorder.view', 'label' => '查看工单', 'group' => '工单套打'],
  18. ['key' => 'workorder.edit', 'label' => '新建 / 编辑工单', 'group' => '工单套打'],
  19. ['key' => 'workorder.delete', 'label' => '删除工单', 'group' => '工单套打'],
  20. ['key' => 'workorder.import', 'label' => '文件导入工单', 'group' => '工单套打'],
  21. ['key' => 'product.view', 'label' => '查看产品', 'group' => '产品工艺'],
  22. ['key' => 'product.edit', 'label' => '新建 / 编辑产品', 'group' => '产品工艺'],
  23. ['key' => 'product.delete', 'label' => '删除产品', 'group' => '产品工艺'],
  24. ['key' => 'product.import', 'label' => '文件导入产品', 'group' => '产品工艺'],
  25. ['key' => 'template.view', 'label' => '查看模板', 'group' => '本厂模板'],
  26. ['key' => 'template.edit', 'label' => '修改本厂模板', 'group' => '本厂模板'],
  27. ['key' => 'template.delete', 'label' => '删除本厂模板', 'group' => '本厂模板'],
  28. ['key' => 'print.do', 'label' => '套打打印', 'group' => '套打'],
  29. ['key' => 'print.view', 'label' => '查看打印记录', 'group' => '套打'],
  30. ['key' => 'print.delete', 'label' => '删除打印记录', 'group' => '套打'],
  31. ['key' => 'market.view', 'label' => '浏览商城', 'group' => '模板商城'],
  32. ['key' => 'market.buy', 'label' => '购买模板', 'group' => '模板商城'],
  33. ];
  34. }
  35. /**
  36. * @return string[]
  37. */
  38. public static function keys()
  39. {
  40. $keys = [];
  41. foreach (self::catalog() as $item) {
  42. $keys[] = $item['key'];
  43. }
  44. return $keys;
  45. }
  46. /**
  47. * @param string $key
  48. * @return string
  49. */
  50. public static function label($key)
  51. {
  52. foreach (self::catalog() as $item) {
  53. if ($item['key'] === $key) {
  54. return $item['label'];
  55. }
  56. }
  57. if ($key === 'factory.view') {
  58. return '查看本厂信息';
  59. }
  60. return $key;
  61. }
  62. /**
  63. * 未单独配置时,操作员默认拥有全部业务权限(与上线前行为一致)
  64. * @return string[]
  65. */
  66. public static function defaults()
  67. {
  68. return array_values(array_unique(array_merge(self::ALWAYS, self::keys())));
  69. }
  70. /**
  71. * @param mixed $raw
  72. * @return string[]|null null 表示沿用默认
  73. */
  74. public static function decode($raw)
  75. {
  76. if ($raw === null || $raw === '') {
  77. return null;
  78. }
  79. if (is_array($raw)) {
  80. $list = $raw;
  81. } else {
  82. $list = json_decode((string)$raw, true);
  83. }
  84. if (!is_array($list)) {
  85. return null;
  86. }
  87. return self::normalize($list, false);
  88. }
  89. /**
  90. * @param array $keys
  91. * @param bool $keepAlways
  92. * @return string[]
  93. */
  94. public static function normalize(array $keys, $keepAlways = true)
  95. {
  96. $allow = array_flip(self::keys());
  97. $out = [];
  98. foreach ($keys as $key) {
  99. $key = trim((string)$key);
  100. if ($key !== '' && isset($allow[$key])) {
  101. $out[$key] = $key;
  102. }
  103. }
  104. $implies = [
  105. 'workorder.edit' => 'workorder.view',
  106. 'workorder.delete' => 'workorder.view',
  107. 'workorder.import' => 'workorder.view',
  108. 'product.edit' => 'product.view',
  109. 'product.delete' => 'product.view',
  110. 'product.import' => 'product.view',
  111. 'template.edit' => 'template.view',
  112. 'template.delete' => 'template.view',
  113. 'print.delete' => 'print.view',
  114. 'market.buy' => 'market.view',
  115. ];
  116. foreach ($implies as $from => $to) {
  117. if (isset($out[$from])) {
  118. $out[$to] = $to;
  119. }
  120. }
  121. if ($keepAlways) {
  122. foreach (self::ALWAYS as $key) {
  123. $out[$key] = $key;
  124. }
  125. }
  126. return array_values($out);
  127. }
  128. /**
  129. * @param mixed $raw
  130. * @return string[]
  131. */
  132. public static function resolve($raw)
  133. {
  134. $decoded = self::decode($raw);
  135. return $decoded === null ? self::defaults() : self::normalize($decoded, true);
  136. }
  137. /**
  138. * @param string $perm
  139. * @return bool
  140. */
  141. public static function can($perm)
  142. {
  143. $perm = trim((string)$perm);
  144. if ($perm === '') {
  145. return true;
  146. }
  147. if (FactoryContext::bypass() || FactoryContext::isApiBound()) {
  148. return true;
  149. }
  150. if (FactoryContext::isOwner()) {
  151. return true;
  152. }
  153. $auth = FrontendAuth::instance();
  154. if (!$auth || !$auth->isLogin()) {
  155. return false;
  156. }
  157. $raw = isset($auth->factory_perms) ? $auth->factory_perms : '';
  158. return in_array($perm, self::resolve($raw), true);
  159. }
  160. /**
  161. * @param array $perms
  162. * @return bool
  163. */
  164. public static function canAny(array $perms)
  165. {
  166. foreach ($perms as $perm) {
  167. if (self::can($perm)) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * @param string $perm
  175. * @return bool
  176. */
  177. public static function deny($perm)
  178. {
  179. return !self::can($perm);
  180. }
  181. /**
  182. * 当前登录成员的有效权限
  183. * @return string[]
  184. */
  185. public static function current()
  186. {
  187. if (FactoryContext::bypass() || FactoryContext::isApiBound() || FactoryContext::isOwner()) {
  188. return array_values(array_unique(array_merge(['*:*:*'], self::defaults())));
  189. }
  190. $auth = FrontendAuth::instance();
  191. if (!$auth || !$auth->isLogin()) {
  192. return self::ALWAYS;
  193. }
  194. $raw = isset($auth->factory_perms) ? $auth->factory_perms : '';
  195. return self::resolve($raw);
  196. }
  197. }