23
23
import com .alibaba .nacos .config .server .model .Page ;
24
24
import com .alibaba .nacos .console .security .nacos .NacosAuthConfig ;
25
25
import com .alibaba .nacos .console .security .nacos .users .NacosUserDetailsServiceImpl ;
26
+ import com .alibaba .nacos .core .auth .AuthConfigs ;
26
27
import com .alibaba .nacos .core .auth .Permission ;
27
28
import com .alibaba .nacos .core .utils .Loggers ;
28
29
import io .jsonwebtoken .lang .Collections ;
44
45
@ Service
45
46
public class NacosRoleServiceImpl {
46
47
47
- private static final String GLOBAL_ADMIN_ROLE = "GLOBAL_ADMIN" ;
48
+ public static final String GLOBAL_ADMIN_ROLE = "GLOBAL_ADMIN" ;
49
+
50
+ @ Autowired
51
+ private AuthConfigs authConfigs ;
48
52
49
53
@ Autowired
50
54
private RolePersistService rolePersistService ;
@@ -95,13 +99,13 @@ private void reload() {
95
99
* Note if the user has many roles, this method returns true if any one role of the user has the
96
100
* desired permission.
97
101
*
98
- * @param username user info
102
+ * @param username user info
99
103
* @param permission permission to auth
100
104
* @return true if granted, false otherwise
101
105
*/
102
106
public boolean hasPermission (String username , Permission permission ) {
103
107
104
- List <RoleInfo > roleInfoList = roleInfoMap . get (username );
108
+ List <RoleInfo > roleInfoList = getRoles (username );
105
109
if (Collections .isEmpty (roleInfoList )) {
106
110
return false ;
107
111
}
@@ -120,7 +124,7 @@ public boolean hasPermission(String username, Permission permission) {
120
124
121
125
// For other roles, use a pattern match to decide if pass or not.
122
126
for (RoleInfo roleInfo : roleInfoList ) {
123
- List <PermissionInfo > permissionInfoList = permissionInfoMap . get (roleInfo .getRole ());
127
+ List <PermissionInfo > permissionInfoList = getPermissions (roleInfo .getRole ());
124
128
if (Collections .isEmpty (permissionInfoList )) {
125
129
continue ;
126
130
}
@@ -136,11 +140,36 @@ public boolean hasPermission(String username, Permission permission) {
136
140
return false ;
137
141
}
138
142
143
+ public List <RoleInfo > getRoles (String username ) {
144
+ List <RoleInfo > roleInfoList = roleInfoMap .get (username );
145
+ if (!authConfigs .isCachingEnabled ()) {
146
+ Page <RoleInfo > roleInfoPage = getRolesFromDatabase (username , 1 , Integer .MAX_VALUE );
147
+ if (roleInfoPage != null ) {
148
+ roleInfoList = roleInfoPage .getPageItems ();
149
+ }
150
+ }
151
+ return roleInfoList ;
152
+ }
153
+
139
154
public Page <RoleInfo > getRolesFromDatabase (String userName , int pageNo , int pageSize ) {
140
155
Page <RoleInfo > roles = rolePersistService .getRolesByUserName (userName , pageNo , pageSize );
156
+ if (roles == null ) {
157
+ return new Page <>();
158
+ }
141
159
return roles ;
142
160
}
143
161
162
+ public List <PermissionInfo > getPermissions (String role ) {
163
+ List <PermissionInfo > permissionInfoList = permissionInfoMap .get (role );
164
+ if (!authConfigs .isCachingEnabled ()) {
165
+ Page <PermissionInfo > permissionInfoPage = getPermissionsFromDatabase (role , 1 , Integer .MAX_VALUE );
166
+ if (permissionInfoPage != null ) {
167
+ permissionInfoList = permissionInfoPage .getPageItems ();
168
+ }
169
+ }
170
+ return permissionInfoList ;
171
+ }
172
+
144
173
public Page <PermissionInfo > getPermissionsByRoleFromDatabase (String role , int pageNo , int pageSize ) {
145
174
return permissionPersistService .getPermissions (role , pageNo , pageSize );
146
175
}
@@ -157,11 +186,15 @@ public void deleteRole(String role, String userName) {
157
186
}
158
187
159
188
public void deleteRole (String role ) {
189
+
160
190
rolePersistService .deleteRole (role );
161
191
}
162
192
163
193
public Page <PermissionInfo > getPermissionsFromDatabase (String role , int pageNo , int pageSize ) {
164
194
Page <PermissionInfo > pageInfo = permissionPersistService .getPermissions (role , pageNo , pageSize );
195
+ if (pageInfo == null ) {
196
+ return new Page <>();
197
+ }
165
198
return pageInfo ;
166
199
}
167
200
0 commit comments