This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
RoleAction.java
124 lines (101 loc) · 3.16 KB
/
RoleAction.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.zanata.action;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Conversation;
import org.zanata.seam.security.IdentityManager;
import org.zanata.security.ZanataIdentity;
import org.zanata.security.annotations.CheckLoggedIn;
import org.zanata.security.annotations.ZanataSecured;
import static org.jboss.seam.ScopeType.CONVERSATION;
import static org.jboss.seam.annotations.Install.APPLICATION;
/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Name("zanataRoleAction")
@Scope(CONVERSATION)
@Install(precedence = APPLICATION)
@ZanataSecured
@CheckLoggedIn
public class RoleAction implements Serializable {
private static final long serialVersionUID = -3830647911484729768L;
private String originalRole;
private String role;
private List<String> groups;
@In
IdentityManager identityManager;
@In
ZanataIdentity identity;
@Begin
public void createRole() {
groups = new ArrayList<>();
}
@Begin
public void editRole(String role) {
this.originalRole = role;
this.role = role;
groups = identityManager.getRoleGroups(role);
}
public String save() {
if (role != null && originalRole != null &&
!role.equals(originalRole)) {
identityManager.deleteRole(originalRole);
}
if (identityManager.roleExists(role)) {
return saveExistingRole();
} else {
return saveNewRole();
}
}
private String saveNewRole() {
boolean success = identityManager.createRole(role);
if (success) {
for (String r : groups) {
identityManager.addRoleToGroup(role, r);
}
Conversation.instance().end();
}
return "success";
}
private String saveExistingRole() {
List<String> grantedRoles = identityManager.getRoleGroups(role);
if (grantedRoles != null) {
for (String r : grantedRoles) {
if (!groups.contains(r)) {
identityManager.removeRoleFromGroup(role, r);
}
}
}
for (String r : groups) {
if (grantedRoles == null || !grantedRoles.contains(r)) {
identityManager.addRoleToGroup(role, r);
}
}
// TODO [CDI] need to end conversation or equivalent scope in CDI
Conversation.instance().end();
return "success";
}
public String getRole() {
return role;
}
public List<String> getAssignableRoles() {
List<String> roles = identityManager.listGrantableRoles();
roles.remove(role);
return roles;
}
public void setRole(String role) {
this.role = role;
}
public List<String> getGroups() {
return groups;
}
public void setGroups(List<String> groups) {
this.groups = groups;
}
}