ludc
2024-10-15 aecacfb404d19749260189ab1d4ee90efc92ae24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.vci.web.util;
import com.vci.client.common.objects.RoleObject;
import com.vci.common.exception.VciException;
import com.vci.corba.common.PLException;
import com.vci.corba.framework.data.FunctionInfo;
import com.vci.corba.framework.data.RoleInfo;
import com.vci.corba.framework.data.RoleRightInfo;
import com.vci.starter.web.pagemodel.SessionInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Component
public class RightControlUtil {
 
 
    @Resource
    private PlatformClientUtil platformClientUtil;
 
    // add by xchao 2012.09.20 统一归整管理员、开发者用户判断
    // 以便将来有可能修改管理员、开发者用户,对于判断依然有效
    private static String userAdminEnum = "user.admin";
    private String userNameAdmin = null;
    private static String userDeveloperEnum = "user.developer";
    private String userNameDeveloper = null;
    private String userNameRoot = null;
    private static String userRootEnum = "user.rooter";
    public boolean isAdmin(String userName){
        return userName.equals(getUserNameAdmin(userAdminEnum));
    }
 
    public boolean isDeveloper(String userName){
        return userName.equals(getUserNameDeveloper(userDeveloperEnum));
    }
 
    public boolean isRoot(String userName){
        return userName.equals(getUserNameRoot(userRootEnum));
    }
 
    /**
     * 是否为管理员、开发者用户
     * @param userName 登录用户名
     * @return 判断结果
     */
    public boolean isAdminOrDeveloperOrRoot(String userName){
        return isAdmin(userName) || isDeveloper(userName)||isRoot(userName);
    }
 
    /**
     * 获取配置的管理员
     * @param key 配置key
     * @return 管理员账号
     */
    public String getUserNameAdmin(String key) {
        if (userNameAdmin == null) {
            synchronized (RightControlUtil.class) {
                if (userNameAdmin == null) {
                    try {
                        userNameAdmin = platformClientUtil.getFrameworkService().getConfigValue(key);
                    }catch (PLException e){
                        log.error(e.code, e.messages);
                    }
                }
            }
        }
        return userNameAdmin;
    }
    /**
     * 获取配置的开发人员
     * @param key 配置key
     * @return 开发人员账号
     */
    public String getUserNameDeveloper(String key) {
        if (userNameDeveloper == null) {
            synchronized (RightControlUtil.class) {
                if (userNameDeveloper == null) {
                    try {
                        userNameDeveloper = platformClientUtil.getFrameworkService().getConfigValue(key);
                    }catch (PLException e){
                        log.error(e.code, e.messages);
                    }
                }
            }
        }
        return userNameDeveloper;
    }
 
    /**
     * 获取配置的管理员
     * @param key 配置key
     * @return 管理员账号
     */
    public String getUserNameRoot(String key) {
        if (userNameRoot == null) {
            synchronized (RightControlUtil.class) {
                if (userNameRoot == null) {
                    try {
                        userNameRoot = platformClientUtil.getFrameworkService().getConfigValue(key);
                    }catch (PLException e){
                        log.error(e.code, e.messages);
                    }
                }
            }
        }
        return userNameRoot;
    }
 
    /**
     * 判断当前是否为三元用户
     * @return
     */
    public boolean isThreeAdminCurUser() {
        SessionInfo sessionInfo = WebUtil.getCurrentUserSessionInfoNotException();
        try {
            RoleInfo[] roles = platformClientUtil.getFrameworkService().fetchRoleInfoByUserId(sessionInfo.getUserOid());
            for (RoleInfo role : roles) {
                if (role.name.equalsIgnoreCase("系统管理员")){
                    return true;
                }
                else if (role.name.equalsIgnoreCase("安全管理员")){
                    return true;
                }
                else if (role.name.equalsIgnoreCase("审计管理员")){
                    return true;
                }
            }
        } catch (PLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 获取当前用户的权限
     * @param userName
     * @return
     */
    public RoleRightInfo[] getRoleRightByUserName(String userName){
        RoleRightInfo[] roleRightByUserName = new RoleRightInfo[0];
        try {
            roleRightByUserName = platformClientUtil.getFrameworkService().getRoleRightByUserName(userName);
        } catch (PLException e) {
            e.printStackTrace();
            log.error(e.code,e.messages);
        }
        return roleRightByUserName;
    }
 
    /**
     * 获取当前用户具有权限的所有模块
     * @param parentId, 父模块id
     * @param userName, 用户名
     * @param userRoleRights, 当前用户具有的权限
     * @return
     */
    public Map<String, List<FunctionInfo>> getAllChildrenFunctionsByUserName(String parentId, String userName, RoleRightInfo[] userRoleRights) {
        Map<String, List<RoleRightInfo>> mapRight = new LinkedHashMap<String, List<RoleRightInfo>>();
        for (int i = 0; i < userRoleRights.length; i++) {
            RoleRightInfo right = userRoleRights[i];
 
            List<RoleRightInfo> lstRight = null;
            if (mapRight.containsKey(right.funcId)) {
                lstRight = mapRight.get(right.funcId);
            } else {
                lstRight = new ArrayList<RoleRightInfo>();
            }
            lstRight.add(right);
            mapRight.put(right.funcId, lstRight);
        }
 
        FunctionInfo[] AllFunction = getFunctionsByParentId(parentId, userName);
        Map<String, List<FunctionInfo>> map = new LinkedHashMap<String, List<FunctionInfo>>();
        boolean isAllShow = isDeveloper(userName) || isAdmin(userName) || !isFunctionSwithOn();
        boolean isHasRight = false;
        FunctionInfo func;
        for (int i = 0; i < AllFunction.length; i++) {
            func = AllFunction[i];
            //System.out.println("=== FUNC:ID=" + func.getId()+ "  ParentID=" + func.getParentId() + ";  Name=" + func.getName());
            isHasRight = false;
            if (isAllShow) {
                isHasRight = true;
            } else {
                List<RoleRightInfo> lstRight = null;
                if (mapRight.containsKey(func.id)) {
                    lstRight = mapRight.get(func.id);
                    //System.out.println("   === 有授权信息");
                }
 
                if (lstRight == null)
                    isHasRight = false;
                else{
                    if (lstRight.size() > 0)
                        isHasRight = true;
//                    for (int j = 0; j < lstRight.size(); j++) {
//                        long lRight = lstRight.get(j).getRightValue();
//                        if (lRight != 0){
//                            isHasRight = true;
//                            //System.out.println("      === 有授权");
//
//                            break;
//                        }
//                    }
                }
            }
 
            if (!isHasRight) {
                continue;
            }
            String cparentId = func.parentId;
 
            //System.out.println("####==== ParentId=" + cparentId + "; Func=" + func.getName());
            List<FunctionInfo> lstFunc = null;
            if (map.containsKey(cparentId)) {
                lstFunc = map.get(cparentId);
            } else {
                lstFunc = new ArrayList<FunctionInfo>();
            }
            lstFunc.add(func);
            map.put(cparentId, lstFunc);
        }
        return map;
    }
 
 
    public FunctionInfo[] getFunctionsByParentId(String parentId, String userName) {
        FunctionInfo[] funcObjs =  null;
        try {
            if(isDeveloper(userName)){
                funcObjs = platformClientUtil.getFrameworkService().getChildrenFunctionsByParentId(parentId, true);
            } else {
                funcObjs =  platformClientUtil.getFrameworkService().getChildrenFunctionsByParentId(parentId, false);
            }
        } catch (PLException e) {
            e.printStackTrace();
            log.error(e.code, e.messages);
        }
        return funcObjs;
    }
 
    /**
     * 判断功能权限是否开启
     * @return
     */
    public boolean isFunctionSwithOn() {
        String functionRightSwith = null;
        try {
            functionRightSwith = platformClientUtil.getFrameworkService().getConfigValue("function.right.swith");
        } catch (PLException e) {
            e.printStackTrace();
            log.error(e.code, e.messages);
        }
        if(functionRightSwith != null && "on".equalsIgnoreCase(functionRightSwith)){
            return true;
        }
 
        return false;
    }
}