-
Notifications
You must be signed in to change notification settings - Fork 11
/
returnValuesPerUserRole.groovy
51 lines (45 loc) · 1.72 KB
/
returnValuesPerUserRole.groovy
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
/*** BEGIN META {
"name" : "Return Values per User Role",
"comment" : "Use the Role Strategy plug-in classes, and according to the currently loggged-in user, return different set of parameters",
"parameters" : [ ''],
"core": "1.580.1",
"authors" : [
{ name : "Bruno P. Kinoshita" }
]
} END META**/
/**
* Use the Role Strategy Plug-in classes, and according to the currently loggged-in user,
* return different set of parameters.
* Author: Bruno P. Kinoshita
* Last Update: 4/24/2016 rev 1
* Required Script Parameters:
* Required Plug-ins: Role Strategy Plug-in
*/
import hudson.model.User
import hudson.model.Hudson
import hudson.security.AuthorizationStrategy
import hudson.security.Permission
import com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy
import com.michelin.cio.hudson.plugins.rolestrategy.RoleMap
AuthorizationStrategy strategy = Hudson.getInstance().getAuthorizationStrategy();
jobs = []
user = User.current()
userId = user.getId()
if (strategy != null && strategy instanceof com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy) {
roleStrategy = (RoleBasedAuthorizationStrategy) strategy;
// not very straightforward to get the groups for a given user
roles = roleStrategy.getGrantedRoles("globalRoles")
for (entry in roles) {
role = entry.key
users = entry.value
if (role.getName().equals("tester")) {
if (userId in users)
return ["PROJECT_FOR_TESTERS1", "PROJECT_FOR_TESTERS2"]
} else if (role.getName().equals("admin")) {
if (userId in users)
return ["PROJECT_FOR_ADMINS1", "PROJECT_FOR_ADMINS2"]
}
}
}
return jobs
// TODO: handle anonymous user ;-)