• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #10030

08 Sep 2023 01:34AM UTC coverage: 47.698%. First build
#10030

push

travis_ci

web-flow
[auth].fix cache info

37 of 37 new or added lines in 8 files covered. (100.0%)

80772 of 169340 relevant lines covered (47.7%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.59
/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/AuthorInfo.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.iotdb.confignode.persistence;
21

22
import org.apache.iotdb.common.rpc.thrift.TSStatus;
23
import org.apache.iotdb.commons.auth.AuthException;
24
import org.apache.iotdb.commons.auth.authorizer.BasicAuthorizer;
25
import org.apache.iotdb.commons.auth.authorizer.IAuthorizer;
26
import org.apache.iotdb.commons.auth.authorizer.OpenIdAuthorizer;
27
import org.apache.iotdb.commons.auth.entity.PathPrivilege;
28
import org.apache.iotdb.commons.auth.entity.Role;
29
import org.apache.iotdb.commons.auth.entity.User;
30
import org.apache.iotdb.commons.conf.CommonConfig;
31
import org.apache.iotdb.commons.conf.CommonDescriptor;
32
import org.apache.iotdb.commons.conf.IoTDBConstant;
33
import org.apache.iotdb.commons.path.PartialPath;
34
import org.apache.iotdb.commons.path.PathPatternTree;
35
import org.apache.iotdb.commons.snapshot.SnapshotProcessor;
36
import org.apache.iotdb.commons.utils.AuthUtils;
37
import org.apache.iotdb.commons.utils.FileUtils;
38
import org.apache.iotdb.commons.utils.TestOnly;
39
import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlanType;
40
import org.apache.iotdb.confignode.consensus.request.auth.AuthorPlan;
41
import org.apache.iotdb.confignode.consensus.response.auth.PermissionInfoResp;
42
import org.apache.iotdb.confignode.rpc.thrift.TAuthizedPatternTreeResp;
43
import org.apache.iotdb.confignode.rpc.thrift.TPathPrivilege;
44
import org.apache.iotdb.confignode.rpc.thrift.TPermissionInfoResp;
45
import org.apache.iotdb.confignode.rpc.thrift.TRoleResp;
46
import org.apache.iotdb.confignode.rpc.thrift.TUserResp;
47
import org.apache.iotdb.rpc.RpcUtils;
48
import org.apache.iotdb.rpc.TSStatusCode;
49

50
import org.apache.thrift.TException;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

54
import java.io.ByteArrayOutputStream;
55
import java.io.DataOutputStream;
56
import java.io.File;
57
import java.io.IOException;
58
import java.nio.ByteBuffer;
59
import java.util.ArrayList;
60
import java.util.HashMap;
61
import java.util.Iterator;
62
import java.util.List;
63
import java.util.Map;
64
import java.util.Set;
65

66
import static org.apache.iotdb.commons.auth.entity.PrivilegeType.isPathRelevant;
67

68
public class AuthorInfo implements SnapshotProcessor {
69

70
  // Works at config node.
71
  private static final Logger logger = LoggerFactory.getLogger(AuthorInfo.class);
1✔
72
  private static final CommonConfig commonConfig = CommonDescriptor.getInstance().getConfig();
1✔
73

74
  private IAuthorizer authorizer;
75

76
  public AuthorInfo() {
1✔
77
    try {
78
      authorizer = BasicAuthorizer.getInstance();
1✔
79
    } catch (AuthException e) {
×
80
      logger.error("get user or role permissionInfo failed because ", e);
×
81
    }
1✔
82
  }
1✔
83

84
  public TPermissionInfoResp login(String username, String password) {
85
    boolean status;
86
    String loginMessage = null;
×
87
    TSStatus tsStatus = new TSStatus();
×
88
    TPermissionInfoResp result = new TPermissionInfoResp();
×
89
    try {
90
      status = authorizer.login(username, password);
×
91
      if (status) {
×
92
        // Bring this user's permission information back to the datanode for caching
93
        if (authorizer instanceof OpenIdAuthorizer) {
×
94
          username = ((OpenIdAuthorizer) authorizer).getIoTDBUserName(username);
×
95
          result = getUserPermissionInfo(username);
×
96
          result.getUserInfo().setIsOpenIdUser(true);
×
97
        } else {
98
          result = getUserPermissionInfo(username);
×
99
        }
100

101
        result.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "Login successfully"));
×
102
      } else {
103
        result = AuthUtils.generateEmptyPermissionInfoResp();
×
104
      }
105
    } catch (AuthException e) {
×
106
      logger.error("meet error while logging in.", e);
×
107
      status = false;
×
108
      loginMessage = e.getMessage();
×
109
    }
×
110
    if (!status) {
×
111
      tsStatus.setMessage(loginMessage != null ? loginMessage : "Authentication failed.");
×
112
      tsStatus.setCode(TSStatusCode.WRONG_LOGIN_PASSWORD.getStatusCode());
×
113
      result.setStatus(tsStatus);
×
114
    }
115
    return result;
×
116
  }
117

118
  // if All paths fail, return No permission
119
  // if some paths fail, return SUCCESS and failed index list
120
  // if all path success, return success and empty index list
121
  public TPermissionInfoResp checkUserPrivileges(
122
      String username, List<PartialPath> paths, int permission) {
123
    boolean status = true;
1✔
124
    TPermissionInfoResp result = new TPermissionInfoResp();
1✔
125
    List<Integer> failedList = new ArrayList<>();
1✔
126
    try {
127
      if (paths.isEmpty()) {
1✔
128
        status = authorizer.checkUserPrivileges(username, null, permission);
1✔
129
      } else {
130
        int pos = 0;
1✔
131
        for (PartialPath path : paths) {
1✔
132
          if (!checkOnePath(username, path, permission)) {
1✔
133
            failedList.add(pos);
1✔
134
          }
135
          pos++;
1✔
136
        }
1✔
137
        if (failedList.size() == paths.size()) {
1✔
138
          status = false;
1✔
139
        }
140
      }
141
    } catch (AuthException e) {
×
142
      status = false;
×
143
    }
1✔
144
    if (status) {
1✔
145
      try {
146
        // Bring this user's permission information back to the datanode for caching
147
        result = getUserPermissionInfo(username);
1✔
148
        result.setFailPos(failedList);
1✔
149
        result.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
1✔
150
      } catch (AuthException e) {
×
151
        result.setStatus(RpcUtils.getStatus(e.getCode(), e.getMessage()));
×
152
      }
1✔
153
    } else {
154
      result = AuthUtils.generateEmptyPermissionInfoResp();
1✔
155
      result.setFailPos(failedList);
1✔
156
      result.setStatus(RpcUtils.getStatus(TSStatusCode.NO_PERMISSION));
1✔
157
    }
158
    return result;
1✔
159
  }
160

161
  private boolean checkOnePath(String username, PartialPath path, int permission)
162
      throws AuthException {
163
    try {
164
      if (authorizer.checkUserPrivileges(username, path, permission)) {
1✔
165
        return true;
1✔
166
      }
167
    } catch (AuthException e) {
×
168
      logger.error("Error occurs when checking the seriesPath {} for user {}", path, username, e);
×
169
      throw new AuthException(e.getCode(), e);
×
170
    }
1✔
171
    return false;
1✔
172
  }
173

174
  public TSStatus authorNonQuery(AuthorPlan authorPlan) {
175
    ConfigPhysicalPlanType authorType = authorPlan.getAuthorType();
1✔
176
    String userName = authorPlan.getUserName();
1✔
177
    String roleName = authorPlan.getRoleName();
1✔
178
    String password = authorPlan.getPassword();
1✔
179
    String newPassword = authorPlan.getNewPassword();
1✔
180
    Set<Integer> permissions = authorPlan.getPermissions();
1✔
181
    boolean grantOpt = authorPlan.getGrantOpt();
1✔
182
    List<PartialPath> nodeNameList = authorPlan.getNodeNameList();
1✔
183
    try {
184
      switch (authorType) {
1✔
185
        case UpdateUser:
186
          authorizer.updateUserPassword(userName, newPassword);
1✔
187
          break;
1✔
188
        case CreateUser:
189
          authorizer.createUser(userName, password);
1✔
190
          break;
1✔
191
        case CreateRole:
192
          authorizer.createRole(roleName);
1✔
193
          break;
1✔
194
        case DropUser:
195
          authorizer.deleteUser(userName);
1✔
196
          break;
1✔
197
        case DropRole:
198
          authorizer.deleteRole(roleName);
1✔
199
          break;
1✔
200
        case GrantRole:
201
          for (int permission : permissions) {
1✔
202
            if (!isPathRelevant(permission)) {
1✔
203
              authorizer.grantPrivilegeToRole(roleName, null, permission, grantOpt);
×
204
              continue;
×
205
            }
206
            for (PartialPath path : nodeNameList) {
1✔
207
              authorizer.grantPrivilegeToRole(roleName, path, permission, grantOpt);
1✔
208
            }
1✔
209
          }
1✔
210
          break;
1✔
211
        case GrantUser:
212
          for (int permission : permissions) {
1✔
213
            if (!isPathRelevant(permission)) {
1✔
214
              authorizer.grantPrivilegeToUser(userName, null, permission, grantOpt);
1✔
215
              continue;
1✔
216
            }
217
            for (PartialPath path : nodeNameList) {
1✔
218
              authorizer.grantPrivilegeToUser(userName, path, permission, grantOpt);
1✔
219
            }
1✔
220
          }
1✔
221
          break;
1✔
222
        case GrantRoleToUser:
223
          authorizer.grantRoleToUser(roleName, userName);
1✔
224
          break;
1✔
225
        case RevokeUser:
226
          for (int permission : permissions) {
1✔
227
            if (!isPathRelevant(permission)) {
1✔
228
              authorizer.revokePrivilegeFromUser(userName, null, permission);
×
229
              continue;
×
230
            }
231
            for (PartialPath path : nodeNameList) {
1✔
232
              authorizer.revokePrivilegeFromUser(userName, path, permission);
1✔
233
            }
1✔
234
          }
1✔
235
          break;
1✔
236
        case RevokeRole:
237
          for (int permission : permissions) {
1✔
238
            if (!isPathRelevant(permission)) {
1✔
239
              authorizer.revokePrivilegeFromRole(roleName, null, permission);
×
240
              continue;
×
241
            }
242
            for (PartialPath path : nodeNameList) {
1✔
243
              authorizer.revokePrivilegeFromRole(roleName, path, permission);
1✔
244
            }
1✔
245
          }
1✔
246
          break;
1✔
247
        case RevokeRoleFromUser:
248
          authorizer.revokeRoleFromUser(roleName, userName);
1✔
249
          break;
1✔
250
        default:
251
          throw new AuthException(
×
252
              TSStatusCode.UNSUPPORTED_AUTH_OPERATION,
253
              "unknown type: " + authorPlan.getAuthorType());
×
254
      }
255
    } catch (AuthException e) {
×
256
      return RpcUtils.getStatus(e.getCode(), e.getMessage());
×
257
    }
1✔
258
    return RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
1✔
259
  }
260

261
  public PermissionInfoResp executeListUsers(AuthorPlan plan) throws AuthException {
262
    PermissionInfoResp result = new PermissionInfoResp();
1✔
263
    List<String> userList = authorizer.listAllUsers();
1✔
264
    if (!plan.getRoleName().isEmpty()) {
1✔
265
      Role role = authorizer.getRole(plan.getRoleName());
1✔
266
      if (role == null) {
1✔
267
        result.setStatus(
×
268
            RpcUtils.getStatus(
×
269
                TSStatusCode.ROLE_NOT_EXIST, "No such role : " + plan.getRoleName()));
×
270
        return result;
×
271
      }
272
      Iterator<String> itr = userList.iterator();
1✔
273
      while (itr.hasNext()) {
1✔
274
        User userObj = authorizer.getUser(itr.next());
1✔
275
        if (userObj == null || !userObj.hasRole(plan.getRoleName())) {
1✔
276
          itr.remove();
1✔
277
        }
278
      }
1✔
279
    }
280
    result.setTag(IoTDBConstant.COLUMN_USER);
1✔
281
    result.setMemberInfo(userList);
1✔
282
    result.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
1✔
283
    return result;
1✔
284
  }
285

286
  public PermissionInfoResp executeListRoles(AuthorPlan plan) throws AuthException {
287
    PermissionInfoResp result = new PermissionInfoResp();
1✔
288
    List<String> permissionInfo = new ArrayList<>();
1✔
289
    List<String> roleList = new ArrayList<>();
1✔
290
    if (plan.getUserName().isEmpty()) {
1✔
291
      roleList.addAll(authorizer.listAllRoles());
1✔
292
    } else {
293
      User user = authorizer.getUser(plan.getUserName());
1✔
294
      if (user == null) {
1✔
295
        result.setStatus(
×
296
            RpcUtils.getStatus(
×
297
                TSStatusCode.USER_NOT_EXIST, "No such user : " + plan.getUserName()));
×
298
        result.setMemberInfo(permissionInfo);
×
299
        return result;
×
300
      }
301
      roleList.addAll(user.getRoleList());
1✔
302
    }
303
    result.setTag(IoTDBConstant.COLUMN_ROLE);
1✔
304
    result.setMemberInfo(roleList);
1✔
305
    result.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
1✔
306
    return result;
1✔
307
  }
308

309
  public PermissionInfoResp executeListRolePrivileges(AuthorPlan plan) throws AuthException {
310
    PermissionInfoResp result = new PermissionInfoResp();
1✔
311
    List<String> permissionInfo = new ArrayList<>();
1✔
312
    Role role = authorizer.getRole(plan.getRoleName());
1✔
313
    if (role == null) {
1✔
314
      result.setStatus(
×
315
          RpcUtils.getStatus(TSStatusCode.ROLE_NOT_EXIST, "No such role : " + plan.getRoleName()));
×
316
      result.setMemberInfo(permissionInfo);
×
317
      return result;
×
318
    }
319
    TPermissionInfoResp resp = new TPermissionInfoResp();
1✔
320
    TRoleResp roleResp = new TRoleResp();
1✔
321
    roleResp.setRoleName(role.getName());
1✔
322
    List<TPathPrivilege> pathList = new ArrayList<>();
1✔
323
    for (PathPrivilege path : role.getPathPrivilegeList()) {
1✔
324
      TPathPrivilege pathPri = new TPathPrivilege();
1✔
325
      pathPri.setPriGrantOpt(path.getGrantOpt());
1✔
326
      pathPri.setPriSet(path.getPrivileges());
1✔
327
      pathPri.setPath(path.getPath().toString());
1✔
328
      pathList.add(pathPri);
1✔
329
    }
1✔
330
    roleResp.setPrivilegeList(pathList);
1✔
331
    roleResp.setSysPriSet(role.getSysPrivilege());
1✔
332
    roleResp.setSysPriSetGrantOpt(role.getSysPriGrantOpt());
1✔
333
    Map<String, TRoleResp> roleInfo = new HashMap<>();
1✔
334
    roleInfo.put(role.getName(), roleResp);
1✔
335
    result.setTag(IoTDBConstant.COLUMN_PRIVILEGE);
1✔
336
    resp.setRoleInfo(roleInfo);
1✔
337
    resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
1✔
338
    result.setPermissionInfoResp(resp);
1✔
339
    result.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
1✔
340
    result.setMemberInfo(permissionInfo);
1✔
341
    return result;
1✔
342
  }
343

344
  public PermissionInfoResp executeListUserPrivileges(AuthorPlan plan) throws AuthException {
345
    PermissionInfoResp result = new PermissionInfoResp();
1✔
346
    User user = authorizer.getUser(plan.getUserName());
1✔
347
    if (user == null) {
1✔
348
      result.setStatus(
×
349
          RpcUtils.getStatus(TSStatusCode.USER_NOT_EXIST, "No such user : " + plan.getUserName()));
×
350
      return result;
×
351
    }
352
    TPermissionInfoResp resp = getUserPermissionInfo(plan.getUserName());
1✔
353
    resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
1✔
354
    result.setTag(IoTDBConstant.COLUMN_PRIVILEGE);
1✔
355
    result.setPermissionInfoResp(resp);
1✔
356
    result.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
1✔
357
    return result;
1✔
358
  }
359

360
  public TAuthizedPatternTreeResp generateAuthizedPTree(String username, int permission)
361
      throws AuthException {
362
    TAuthizedPatternTreeResp resp = new TAuthizedPatternTreeResp();
×
363
    User user = authorizer.getUser(username);
×
364
    PathPatternTree pPtree = new PathPatternTree();
×
365
    if (user == null) {
×
366
      resp.setStatus(RpcUtils.getStatus(TSStatusCode.USER_NOT_EXIST, "No such user : " + username));
×
367
      resp.setUsername(username);
×
368
      resp.setPrivilegeId(permission);
×
369
      return resp;
×
370
    }
371
    for (PathPrivilege path : user.getPathPrivilegeList()) {
×
372
      if (path.checkPrivilege(permission)) {
×
373
        pPtree.appendPathPattern(path.getPath());
×
374
      }
375
    }
×
376
    for (String rolename : user.getRoleList()) {
×
377
      Role role = authorizer.getRole(rolename);
×
378
      if (role != null) {
×
379
        for (PathPrivilege path : role.getPathPrivilegeList()) {
×
380
          if (path.checkPrivilege(permission)) {
×
381
            pPtree.appendPathPattern(path.getPath());
×
382
          }
383
        }
×
384
      }
385
    }
×
386
    pPtree.constructTree();
×
387
    resp.setUsername(username);
×
388
    resp.setPrivilegeId(permission);
×
389
    resp.setStatus(new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()));
×
390
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
×
391
    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
×
392
    try {
393
      pPtree.serialize(dataOutputStream);
×
394
    } catch (IOException e) {
×
395
      resp.setStatus(
×
396
          RpcUtils.getStatus(
×
397
              TSStatusCode.EXECUTE_STATEMENT_ERROR, "Get error when serialize pattern tree."));
398
      return resp;
×
399
    }
×
400
    resp.setPathPatternTree(ByteBuffer.wrap(byteArrayOutputStream.toByteArray()));
×
401
    resp.setPermissionInfo(getUserPermissionInfo(username));
×
402
    return resp;
×
403
  }
404

405
  public TPermissionInfoResp checkUserPrivilegeGrantOpt(
406
      String username, List<PartialPath> paths, int permission) throws AuthException {
407
    User user = authorizer.getUser(username);
×
408
    TPermissionInfoResp resp = new TPermissionInfoResp();
×
409
    boolean status = false;
×
410
    if (user == null) {
×
411
      resp.setStatus(RpcUtils.getStatus(TSStatusCode.USER_NOT_EXIST, "No such user : " + username));
×
412
      return resp;
×
413
    }
414
    try {
415
      if (isPathRelevant(permission)) {
×
416
        for (PartialPath path : paths) {
×
417
          if (user.checkPathPrivilegeGrantOpt(path, permission)) {
×
418
            status = true;
×
419
            continue;
×
420
          }
421
          if (!status) {
×
422
            for (String roleName : user.getRoleList()) {
×
423
              Role role = authorizer.getRole(roleName);
×
424
              if (role.checkPathPrivilegeGrantOpt(path, permission)) {
×
425
                status = true;
×
426
                break;
×
427
              }
428
            }
×
429
          }
430
          if (!status) {
×
431
            break;
×
432
          }
433
        }
×
434
      } else {
435
        if (user.getSysPrivilege().contains(permission)
×
436
            && user.getSysPriGrantOpt().contains(permission)) {
×
437
          status = true;
×
438
        }
439
        if (!status) {
×
440
          for (String roleName : user.getRoleList()) {
×
441
            Role role = authorizer.getRole(roleName);
×
442
            if (role.getSysPrivilege().contains(permission)
×
443
                && role.getSysPriGrantOpt().contains(permission)) {
×
444
              status = true;
×
445
              break;
×
446
            }
447
          }
×
448
        }
449
      }
450
    } catch (AuthException e) {
×
451
      status = false;
×
452
    }
×
453
    if (status) {
×
454
      try {
455
        // Bring this user's permission information back to the datanode for caching
456
        resp = getUserPermissionInfo(username);
×
457
        resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
×
458
      } catch (AuthException e) {
×
459
        resp.setStatus(RpcUtils.getStatus(e.getCode(), e.getMessage()));
×
460
      }
×
461
    } else {
462
      resp = AuthUtils.generateEmptyPermissionInfoResp();
×
463
      resp.setStatus(RpcUtils.getStatus(TSStatusCode.NO_PERMISSION));
×
464
    }
465

466
    return resp;
×
467
  }
468

469
  public TPermissionInfoResp checkRoleOfUser(String username, String rolename)
470
      throws AuthException {
471
    TPermissionInfoResp result = new TPermissionInfoResp();
×
472
    User user = authorizer.getUser(username);
×
473
    if (user == null) {
×
474
      throw new AuthException(
×
475
          TSStatusCode.USER_NOT_EXIST, String.format("No such user : %s", username));
×
476
    }
477
    result = getUserPermissionInfo(username);
×
478
    if (user.getRoleList().contains(rolename)) {
×
479
      result.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
×
480
    } else {
481
      result.setStatus(RpcUtils.getStatus(TSStatusCode.USER_NOT_HAS_ROLE));
×
482
    }
483
    return result;
×
484
  }
485

486
  @Override
487
  public boolean processTakeSnapshot(File snapshotDir) throws TException, IOException {
488
    return authorizer.processTakeSnapshot(snapshotDir);
1✔
489
  }
490

491
  @Override
492
  public void processLoadSnapshot(File snapshotDir) throws TException, IOException {
493
    authorizer.processLoadSnapshot(snapshotDir);
1✔
494
    try {
495
      authorizer.reset();
1✔
496
    } catch (AuthException e) {
×
497
      throw new IOException("Error when load role and user: %s", e);
×
498
    }
1✔
499
  }
1✔
500

501
  @TestOnly
502
  public void clear() throws AuthException {
503
    File userFolder = new File(commonConfig.getUserFolder());
1✔
504
    if (userFolder.exists()) {
1✔
505
      FileUtils.deleteDirectory(userFolder);
1✔
506
    }
507
    File roleFolder = new File(commonConfig.getRoleFolder());
1✔
508
    if (roleFolder.exists()) {
1✔
509
      FileUtils.deleteDirectory(roleFolder);
1✔
510
    }
511
    authorizer.reset();
1✔
512
  }
1✔
513

514
  /**
515
   * Save the user's permission information,Bring back the DataNode for caching
516
   *
517
   * @param username The username of the user that needs to be cached
518
   */
519
  public TPermissionInfoResp getUserPermissionInfo(String username) throws AuthException {
520
    TPermissionInfoResp result = new TPermissionInfoResp();
1✔
521
    TUserResp tUserResp = new TUserResp();
1✔
522
    Map<String, TRoleResp> tRoleRespMap = new HashMap();
1✔
523
    List<TPathPrivilege> userPrivilegeList = new ArrayList<>();
1✔
524

525
    // User permission information
526
    User user = authorizer.getUser(username);
1✔
527
    if (user.getPathPrivilegeList() != null) {
1✔
528
      for (PathPrivilege pathPrivilege : user.getPathPrivilegeList()) {
1✔
529
        TPathPrivilege path = new TPathPrivilege();
1✔
530
        path.setPath(pathPrivilege.getPath().getFullPath());
1✔
531
        path.setPriSet(pathPrivilege.getPrivileges());
1✔
532
        path.setPriGrantOpt(pathPrivilege.getGrantOpt());
1✔
533
        userPrivilegeList.add(path);
1✔
534
      }
1✔
535
    }
536
    tUserResp.setUsername(user.getName());
1✔
537
    tUserResp.setPassword(user.getPassword());
1✔
538
    tUserResp.setPrivilegeList(userPrivilegeList);
1✔
539
    tUserResp.setRoleList(user.getRoleList());
1✔
540
    tUserResp.setSysPriSet(user.getSysPrivilege());
1✔
541
    tUserResp.setSysPriSetGrantOpt(user.getSysPriGrantOpt());
1✔
542

543
    // Permission information for roles owned by users
544
    if (user.getRoleList() != null) {
1✔
545
      for (String roleName : user.getRoleList()) {
1✔
546
        Role role = authorizer.getRole(roleName);
1✔
547
        List<TPathPrivilege> rolePrivilegeList = new ArrayList<>();
1✔
548
        for (PathPrivilege pathPrivilege : role.getPathPrivilegeList()) {
1✔
549
          TPathPrivilege path = new TPathPrivilege();
1✔
550
          path.setPath(pathPrivilege.getPath().getFullPath());
1✔
551
          path.setPriSet(pathPrivilege.getPrivileges());
1✔
552
          path.setPriGrantOpt(pathPrivilege.getGrantOpt());
1✔
553
          rolePrivilegeList.add(path);
1✔
554
        }
1✔
555
        tRoleRespMap.put(
1✔
556
            roleName,
557
            new TRoleResp(
558
                roleName, rolePrivilegeList, role.getSysPrivilege(), role.getSysPriGrantOpt()));
1✔
559
      }
1✔
560
    }
561
    result.setUserInfo(tUserResp);
1✔
562
    result.setRoleInfo(tRoleRespMap);
1✔
563
    result.setStatus(new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()));
1✔
564
    return result;
1✔
565
  }
566
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc