| /** |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| |
| import React from 'react'; |
| import PromiseRender from './PromiseRender'; |
| import { CURRENT } from './index'; |
| |
| function isPromise(obj) { |
| return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; |
| } |
| |
| /** |
| * Common check permissions method |
| * @param { 权限判定 Permission judgment type string |array | Promise | Function } authority |
| * @param { 你的权限 Your permission description type:string} currentAuthority |
| * @param { 通过的组件 Passing components } target |
| * @param { 未通过的组件 no pass components } Exception |
| */ |
| const checkPermissions = (authority, currentAuthority, target, Exception) => { |
| // Retirement authority, return target; |
| if (!authority) { |
| return target; |
| } |
| if (Array.isArray(authority)) { |
| if (authority.indexOf(currentAuthority) >= 0) { |
| return target; |
| } |
| return Exception; |
| } |
| |
| if (typeof authority === 'string') { |
| if (authority === currentAuthority) { |
| return target; |
| } |
| return Exception; |
| } |
| |
| if (isPromise(authority)) { |
| return () => ( |
| <PromiseRender ok={target} error={Exception} promise={authority} /> |
| ); |
| } |
| |
| // Function 处理 |
| if (typeof authority === 'function') { |
| try { |
| const bool = authority(currentAuthority); |
| if (bool) { |
| return target; |
| } |
| return Exception; |
| } catch (error) { |
| throw error; |
| } |
| } |
| throw new Error('unsupported parameters'); |
| }; |
| |
| export { checkPermissions }; |
| |
| const check = (authority, target, Exception) => { |
| return checkPermissions(authority, CURRENT, target, Exception); |
| }; |
| |
| export default check; |