-
Notifications
You must be signed in to change notification settings - Fork 1
/
export-role-strategy-permissions-to-csv.groovy
86 lines (72 loc) · 2.74 KB
/
export-role-strategy-permissions-to-csv.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
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
/*** BEGIN META {
"name" : "Export role-strategy permissions to CSV",
"comment" : "Export the permissions defined in the <a href='https://wiki.jenkins-ci.org/display/JENKINS/Role+Strategy+Plugin'>role-strategy plugin</a> in a CSV format. <br />Further information in the ticket <a href='https://issues.jenkins-ci.org/browse/JENKINS-8075'>JENKINS-8075</a>",
"parameters" : [],
"core": "1.424.2",
"authors" : [
{ name : "Daniel PETISME <danielpetisme> <[email protected]>" }
]
} END META**/
import hudson.model.Hudson
import com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy
def cleanUsers = { it.flatten().sort().unique() - "null"}
/*
* UI part
* The matrix is composed by the match between the roles (columns) and the users (rows).
* Basically, you can start with this data structure to generate an export in the format you want.
*/
def export = {matrix, formatter -> formatter(matrix)}
/**
* The default CSV formatter
*/
def csv = { matrix -> matrix.collect{ key, value -> "\n$key, ${value.join(",").replace("true", "x").replace("false", " ")}" } + "\n" }
/*
* The script only work with the role-strategy plugin
* https://wiki.jenkins-ci.org/display/JENKINS/Role+Strategy+Plugin
*/
def authStrategy = Hudson.instance.getAuthorizationStrategy()
if(authStrategy instanceof RoleBasedAuthorizationStrategy){
/*
* Get a [role]:[users] map
*/
def permissions = authStrategy.roleMaps.inject([:]){map, it -> map + it.value.grantedRoles}
/*
* Get all the users defined in the role-strategy plugin
*/
def users = cleanUsers(permissions*.value)
/*
* Get a [user]:[roles] map
*/
def permissionsByUser = users.inject([:]){ map, user ->
map[user] = permissions.findAll{ it.value.contains(user)}.collect{it.key.name}
map
}
/*
* The matrix building
*/
def usersPermissionsMatrix =[:]
/*
* Get all the roles defined in the role-strategy plugin
*/
def roles = authStrategy.getRoleMap(authStrategy.GLOBAL).grantedRoles*.key.name.sort() + authStrategy.getRoleMap(authStrategy.PROJECT).grantedRoles*.key.name.sort()
usersPermissionsMatrix["roles"] = roles
/*
* Algo:
* For each user
* For each role
* matrix[user][role] = hasPermission(user, role)
* Done
* Done
*/
users.each{ user ->
usersPermissionsMatrix[user] = roles.inject([]){ list, role ->
list << permissionsByUser[user].contains(role)
}
}
/*
* We're done!! it's time to export the work.
*/
println export(usersPermissionsMatrix, csv)
}else{
println "Not able to list the permissions by user"
}