|
16 | 16 | // under the License.
|
17 | 17 | package org.apache.cloudstack.acl;
|
18 | 18 |
|
| 19 | +import com.cloud.user.Account; |
| 20 | +import com.google.common.base.Enums; |
| 21 | +import com.google.common.base.Strings; |
| 22 | + |
19 | 23 | // Enum for default roles in CloudStack
|
20 | 24 | public enum RoleType {
|
21 |
| - Admin(1), ResourceAdmin(2), DomainAdmin(4), User(8), Unknown(0); |
| 25 | + Admin(1L, Account.ACCOUNT_TYPE_ADMIN, 1), |
| 26 | + ResourceAdmin(2L, Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN, 2), |
| 27 | + DomainAdmin(3L, Account.ACCOUNT_TYPE_DOMAIN_ADMIN, 4), |
| 28 | + User(4L, Account.ACCOUNT_TYPE_NORMAL, 8), |
| 29 | + Unknown(-1L, (short) -1, 0); |
22 | 30 |
|
| 31 | + private long id; |
| 32 | + private short accountType; |
23 | 33 | private int mask;
|
24 | 34 |
|
25 |
| - private RoleType(int mask) { |
| 35 | + RoleType(final long id, final short accountType, final int mask) { |
| 36 | + this.id = id; |
| 37 | + this.accountType = accountType; |
26 | 38 | this.mask = mask;
|
27 | 39 | }
|
28 | 40 |
|
29 |
| - public int getValue() { |
| 41 | + public long getId() { |
| 42 | + return id; |
| 43 | + } |
| 44 | + |
| 45 | + public short getAccountType() { |
| 46 | + return accountType; |
| 47 | + } |
| 48 | + |
| 49 | + public int getMask() { |
30 | 50 | return mask;
|
31 | 51 | }
|
32 |
| -} |
33 | 52 |
|
| 53 | + public static RoleType fromString(final String name) { |
| 54 | + if (!Strings.isNullOrEmpty(name) |
| 55 | + && Enums.getIfPresent(RoleType.class, name).isPresent()) { |
| 56 | + return RoleType.valueOf(name); |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + public static RoleType fromMask(int mask) { |
| 62 | + for (RoleType roleType : RoleType.values()) { |
| 63 | + if (roleType.getMask() == mask) { |
| 64 | + return roleType; |
| 65 | + } |
| 66 | + } |
| 67 | + return Unknown; |
| 68 | + } |
| 69 | + |
| 70 | + public static RoleType getByAccountType(final short accountType) { |
| 71 | + RoleType roleType = RoleType.Unknown; |
| 72 | + switch (accountType) { |
| 73 | + case Account.ACCOUNT_TYPE_ADMIN: |
| 74 | + roleType = RoleType.Admin; |
| 75 | + break; |
| 76 | + case Account.ACCOUNT_TYPE_DOMAIN_ADMIN: |
| 77 | + roleType = RoleType.DomainAdmin; |
| 78 | + break; |
| 79 | + case Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN: |
| 80 | + roleType = RoleType.ResourceAdmin; |
| 81 | + break; |
| 82 | + case Account.ACCOUNT_TYPE_NORMAL: |
| 83 | + roleType = RoleType.User; |
| 84 | + break; |
| 85 | + } |
| 86 | + return roleType; |
| 87 | + } |
| 88 | + |
| 89 | + public static Long getRoleByAccountType(final Long roleId, final Short accountType) { |
| 90 | + if (roleId == null && accountType != null) { |
| 91 | + RoleType defaultRoleType = RoleType.getByAccountType(accountType); |
| 92 | + if (defaultRoleType != null && defaultRoleType != RoleType.Unknown) { |
| 93 | + return defaultRoleType.getId(); |
| 94 | + } |
| 95 | + } |
| 96 | + return roleId; |
| 97 | + } |
| 98 | + |
| 99 | + public static Short getAccountTypeByRole(final Role role, final Short accountType) { |
| 100 | + if (role != null && role.getId() > 0L) { |
| 101 | + return role.getRoleType().getAccountType(); |
| 102 | + } |
| 103 | + return accountType; |
| 104 | + } |
| 105 | +} |
0 commit comments