forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoleType.java
More file actions
105 lines (93 loc) · 3.46 KB
/
RoleType.java
File metadata and controls
105 lines (93 loc) · 3.46 KB
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
// 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.
package org.apache.cloudstack.acl;
import com.cloud.user.Account;
import com.google.common.base.Enums;
import com.google.common.base.Strings;
// Enum for default roles in CloudStack
public enum RoleType {
Admin(1L, Account.ACCOUNT_TYPE_ADMIN, 1),
ResourceAdmin(2L, Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN, 2),
DomainAdmin(3L, Account.ACCOUNT_TYPE_DOMAIN_ADMIN, 4),
User(4L, Account.ACCOUNT_TYPE_NORMAL, 8),
Unknown(-1L, (short) -1, 0);
private long id;
private short accountType;
private int mask;
RoleType(final long id, final short accountType, final int mask) {
this.id = id;
this.accountType = accountType;
this.mask = mask;
}
public long getId() {
return id;
}
public short getAccountType() {
return accountType;
}
public int getMask() {
return mask;
}
public static RoleType fromString(final String name) {
if (!Strings.isNullOrEmpty(name)
&& Enums.getIfPresent(RoleType.class, name).isPresent()) {
return RoleType.valueOf(name);
}
throw new IllegalStateException("Illegal RoleType name provided");
}
public static RoleType fromMask(int mask) {
for (RoleType roleType : RoleType.values()) {
if (roleType.getMask() == mask) {
return roleType;
}
}
return Unknown;
}
public static RoleType getByAccountType(final short accountType) {
RoleType roleType = RoleType.Unknown;
switch (accountType) {
case Account.ACCOUNT_TYPE_ADMIN:
roleType = RoleType.Admin;
break;
case Account.ACCOUNT_TYPE_DOMAIN_ADMIN:
roleType = RoleType.DomainAdmin;
break;
case Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN:
roleType = RoleType.ResourceAdmin;
break;
case Account.ACCOUNT_TYPE_NORMAL:
roleType = RoleType.User;
break;
}
return roleType;
}
public static Long getRoleByAccountType(final Long roleId, final Short accountType) {
if (roleId == null && accountType != null) {
RoleType defaultRoleType = RoleType.getByAccountType(accountType);
if (defaultRoleType != null && defaultRoleType != RoleType.Unknown) {
return defaultRoleType.getId();
}
}
return roleId;
}
public static Short getAccountTypeByRole(final Role role, final Short accountType) {
if (role != null && role.getId() > 0L) {
return role.getRoleType().getAccountType();
}
return accountType;
}
}