Blame view

ant-design-vue-jeecg/src/utils/authFilter.js 7.23 KB
肖超群 authored
1
import {USER_AUTH, SYS_BUTTON_AUTH} from "@/store/mutation-types"
肖超群 authored
2
肖超群 authored
3
4
export function disabledAuthFilter(code, formData) {
  if (nodeDisabledAuth(code, formData)) {
肖超群 authored
5
    return true;
肖超群 authored
6
  } else {
肖超群 authored
7
8
9
10
    return globalDisabledAuth(code);
  }
}
肖超群 authored
11
function nodeDisabledAuth(code, formData) {
肖超群 authored
12
13
14
15
16
17
  //console.log("页面权限禁用--NODE--开始");
  let permissionList = [];
  try {
    //console.log("页面权限禁用--NODE--开始",formData);
    if (formData) {
      let bpmList = formData.permissionList;
肖超群 authored
18
      permissionList = bpmList.filter(item => item.type == '2')
肖超群 authored
19
20
21
22
23
      // for (let bpm of bpmList) {
      //   if(bpm.type == '2') {
      //     permissionList.push(bpm);
      //   }
      // }
肖超群 authored
24
    } else {
肖超群 authored
25
26
27
28
29
      return false;
    }
  } catch (e) {
    //console.log("页面权限异常----", e);
  }
肖超群 authored
30
  if (permissionList.length == 0) {
肖超群 authored
31
32
33
34
35
36
    return false;
  }

  console.log("流程节点页面权限禁用--NODE--开始");
  let permissions = [];
  for (let item of permissionList) {
肖超群 authored
37
    if (item.type == '2') {
肖超群 authored
38
39
40
41
42
43
      permissions.push(item.action);
    }
  }
  //console.log("页面权限----"+code);
  if (!permissions.includes(code)) {
    return false;
肖超群 authored
44
  } else {
肖超群 authored
45
    for (let item2 of permissionList) {
肖超群 authored
46
      if (code === item2.action) {
肖超群 authored
47
48
49
50
51
52
53
54
        console.log("流程节点页面权限禁用--NODE--生效");
        return true;
      }
    }
  }
  return false;
}
肖超群 authored
55
function globalDisabledAuth(code) {
肖超群 authored
56
57
58
59
60
61
62
63
  //console.log("全局页面禁用权限--Global--开始");

  let permissionList = [];
  let allPermissionList = [];

  //let authList = Vue.ls.get(USER_AUTH);
  let authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || "[]");
  for (let auth of authList) {
肖超群 authored
64
    if (auth.type == '2') {
肖超群 authored
65
66
67
68
69
70
      permissionList.push(auth);
    }
  }
  //console.log("页面禁用权限--Global--",sessionStorage.getItem(SYS_BUTTON_AUTH));
  let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || "[]");
  for (let gauth of allAuthList) {
肖超群 authored
71
    if (gauth.type == '2') {
肖超群 authored
72
73
74
75
      allPermissionList.push(gauth);
    }
  }
  //设置全局配置是否有命中
肖超群 authored
76
  let gFlag = false;//禁用命中
肖超群 authored
77
  let invalidFlag = false;//无效命中
肖超群 authored
78
  if (allPermissionList != null && allPermissionList != "" && allPermissionList != undefined && allPermissionList.length > 0) {
肖超群 authored
79
    for (let itemG of allPermissionList) {
肖超群 authored
80
81
      if (code === itemG.action) {
        if (itemG.status == '0') {
肖超群 authored
82
83
          invalidFlag = true;
          break;
肖超群 authored
84
        } else {
肖超群 authored
85
86
87
88
89
90
          gFlag = true;
          break;
        }
      }
    }
  }
肖超群 authored
91
  if (invalidFlag) {
肖超群 authored
92
93
    return false;
  }
肖超群 authored
94
  if (permissionList === null || permissionList === "" || permissionList === undefined || permissionList.length <= 0) {
肖超群 authored
95
96
97
98
    return gFlag;
  }
  let permissions = [];
  for (let item of permissionList) {
肖超群 authored
99
    if (item.type == '2') {
肖超群 authored
100
101
102
103
104
105
      permissions.push(item.action);
    }
  }
  //console.log("页面禁用权限----"+code);
  if (!permissions.includes(code)) {
    return gFlag;
肖超群 authored
106
  } else {
肖超群 authored
107
    for (let item2 of permissionList) {
肖超群 authored
108
      if (code === item2.action) {
肖超群 authored
109
110
111
112
113
114
115
116
117
        //console.log("全局页面权限解除禁用--Global--生效");
        gFlag = false;
      }
    }
    return gFlag;
  }
}
肖超群 authored
118
export function colAuthFilter(columns, pre) {
肖超群 authored
119
120
  let authList = getNoAuthCols(pre);
  const cols = columns.filter(item => {
肖超群 authored
121
    if (hasColoum(item, authList)) {
肖超群 authored
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
      return true
    }
    return false
  })
  return cols
}

/**
 * 【子表行编辑】实现两个功能:
 * 1、隐藏JEditableTable无权限的字段
 * 2、禁用JEditableTable无权限的字段
 * @param columns
 * @param pre
 * @returns {*}
 */
肖超群 authored
137
export function colAuthFilterJEditableTable(columns, pre) {
肖超群 authored
138
139
140
141
142
  let authList = getAllShowAndDisabledAuthCols(pre);
  const cols = columns.filter(item => {
    let oneAuth = authList.find(auth => {
      return auth.action === pre + item.key;
    });
肖超群 authored
143
    if (!oneAuth) {
肖超群 authored
144
145
146
147
      return true
    }

    //代码严谨处理,防止一个授权标识,配置多次
肖超群 authored
148
    if (oneAuth instanceof Array) {
肖超群 authored
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      oneAuth = oneAuth[0]
    }

    //禁用逻辑
    if (oneAuth.type == '2' && !oneAuth.isAuth) {
      item["disabled"] = true
      return true
    }
    //隐藏逻辑逻辑
    if (oneAuth.type == '1' && !oneAuth.isAuth) {
      return false
    }
    return true
  })
  return cols
}
肖超群 authored
167
function hasColoum(item, authList) {
肖超群 authored
168
169
170
171
172
173
174
175
  if (authList.includes(item.dataIndex)) {
    return false
  }
  return true
}

//权限无效时不做控制,有效时控制,只能控制 显示不显示
//根据授权码前缀获取未授权的列信息
肖超群 authored
176
177
export function getNoAuthCols(pre) {
  if (!pre || pre.length == 0) {
肖超群 authored
178
179
180
181
182
183
184
185
186
    return []
  }
  let permissionList = [];
  let allPermissionList = [];

  //let authList = Vue.ls.get(USER_AUTH);
  let authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || "[]");
  for (let auth of authList) {
    //显示策略,有效状态
肖超群 authored
187
188
    if (auth.type == '1' && startWith(auth.action, pre)) {
      permissionList.push(substrPre(auth.action, pre));
肖超群 authored
189
190
191
192
193
194
    }
  }
  //console.log("页面禁用权限--Global--",sessionStorage.getItem(SYS_BUTTON_AUTH));
  let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || "[]");
  for (let gauth of allAuthList) {
    //显示策略,有效状态
肖超群 authored
195
196
    if (gauth.type == '1' && gauth.status == '1' && startWith(gauth.action, pre)) {
      allPermissionList.push(substrPre(gauth.action, pre));
肖超群 authored
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    }
  }
  const cols = allPermissionList.filter(item => {
    if (permissionList.includes(item)) {
      return false;
    }
    return true;
  })
  return cols;
}

/**
 * 将Online的行编辑按钮权限,添加至本地存储
 */
肖超群 authored
211
export function addOnlineBtAuth2Storage(pre, authList) {
肖超群 authored
212
  let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || "[]");
肖超群 authored
213
214
  let newAuthList = allAuthList.filter(item => {
    if (!item.action) {
肖超群 authored
215
216
      return true
    }
肖超群 authored
217
    return item.action.indexOf(pre) < 0
肖超群 authored
218
  })
肖超群 authored
219
220
  if (authList && authList.length > 0) {
    for (let item of authList) {
肖超群 authored
221
      newAuthList.push({
肖超群 authored
222
223
224
        action: pre + item,
        type: 1,
        status: 1
肖超群 authored
225
226
227
      })
    }
    let temp = JSON.parse(sessionStorage.getItem(USER_AUTH) || "[]");
肖超群 authored
228
229
    let newArr = temp.filter(item => {
      if (!item.action) {
肖超群 authored
230
231
        return true
      }
肖超群 authored
232
      return item.action.indexOf(pre) < 0 || authList.indexOf(item.action.replace(pre, '')) < 0
肖超群 authored
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    })
    sessionStorage.setItem(USER_AUTH, JSON.stringify(newArr))
  }
  sessionStorage.setItem(SYS_BUTTON_AUTH, JSON.stringify(newAuthList))
}


/**
 * 额外增加方法【用于行编辑组件】
 * date: 2020-04-05
 * author: scott
 * @param pre
 * @returns {*[]}
 */
肖超群 authored
247
function getAllShowAndDisabledAuthCols(pre) {
肖超群 authored
248
249
250
251
252
253
  //用户拥有的权限
  let userAuthList = JSON.parse(sessionStorage.getItem(USER_AUTH) || "[]");
  //全部权限配置
  let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || "[]");

  let newAllAuthList = allAuthList.map(function (item, index) {
肖超群 authored
254
255
    let hasAuthArray = userAuthList.filter(u => u.action === item.action);
    if (hasAuthArray && hasAuthArray.length > 0) {
肖超群 authored
256
257
258
259
260
261
262
263
      item["isAuth"] = true
    }
    return item;
  })

  return newAllAuthList;
}
肖超群 authored
264
265
function startWith(str, pre) {
  if (pre == null || pre == "" || str == null || str == "" || str.length == 0 || pre.length > str.length)
肖超群 authored
266
267
268
269
270
271
272
    return false;
  if (str.substr(0, pre.length) == pre)
    return true;
  else
    return false;
}
肖超群 authored
273
function substrPre(str, pre) {
肖超群 authored
274
275
  return str.substr(pre.length);
}