1 package net.sourceforge.blogentis.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import net.sourceforge.blogentis.om.Blog;
30 import net.sourceforge.blogentis.turbine.BlogParameterParser;
31 import net.sourceforge.blogentis.turbine.SecureBlogScreen;
32 import net.sourceforge.blogentis.utils.BlogConstants;
33
34 import org.apache.ecs.html.Option;
35 import org.apache.ecs.html.Select;
36 import org.apache.torque.util.Criteria;
37 import org.apache.turbine.om.security.Group;
38 import org.apache.turbine.om.security.Role;
39 import org.apache.turbine.om.security.User;
40 import org.apache.turbine.om.security.peer.RolePeer;
41 import org.apache.turbine.services.security.TurbineSecurity;
42 import org.apache.turbine.util.RunData;
43 import org.apache.turbine.util.security.AccessControlList;
44 import org.apache.turbine.util.security.DataBackendException;
45 import org.apache.turbine.util.security.RoleSet;
46 import org.apache.turbine.util.security.UnknownEntityException;
47 import org.apache.velocity.context.Context;
48
49 public class BlogUsers extends SecureBlogScreen {
50 public static class UserRole {
51 public User user;
52
53 public Role role;
54
55 UserRole(User user, Role role) {
56 this.user = user;
57 this.role = role;
58 }
59
60 public Role getRole() {
61 return role;
62 }
63
64 public User getUser() {
65 return user;
66 }
67 }
68
69 public static abstract class ItemList {
70 protected List list = null;
71
72 protected String selected = null;
73
74 private String name = "select";
75
76 public ItemList(List items) {
77 list = items;
78 }
79
80 public String toString() {
81 Select s = new Select();
82 s.setName(name);
83 for(Iterator i = list.iterator(); i.hasNext();) {
84 Option o = new Option();
85 Object ob = i.next();
86 doItem(o, ob);
87 s.addElement(o);
88 }
89 return s.toString();
90 }
91
92 public abstract void doItem(Option o, Object ob);
93
94 public String getName() {
95 return name;
96 }
97
98 public ItemList setName(String string) {
99 name = string;
100 return this;
101 }
102 }
103
104 public static class RoleList extends ItemList {
105 public RoleList(List roles) {
106 super(roles);
107 }
108
109 public ItemList setRole(String name) {
110 this.selected = name;
111 return this;
112 }
113
114 public ItemList setRole(Role role) {
115 this.selected = role.getName();
116 return this;
117 }
118
119 public void doItem(Option o, Object ob) {
120 Role r = (Role)ob;
121 o.setValue(r.getName());
122 o.addElement(r.getName());
123 if (r.getName().equals(selected))
124 o.setSelected(true);
125 }
126 }
127
128 public static class UserList extends ItemList {
129 public UserList(List users) {
130 super(users);
131 }
132
133 public ItemList setUser(String name) {
134 this.selected = name;
135 return this;
136 }
137
138 public ItemList setUser(User user) {
139 this.selected = user.getName();
140 return this;
141 }
142
143 public void doItem(Option o, Object ob) {
144 User u = (User)ob;
145 o.setValue(u.getName());
146 o.addElement(u.getFirstName() + " " + u.getLastName());
147 if (u.getName().equals(selected))
148 o.setSelected(true);
149 }
150 }
151
152 private ItemList getRoleList()
153 throws DataBackendException {
154 RoleSet set = TurbineSecurity.getRoles(new Criteria().add(
155 RolePeer.ROLE_ID, 0, Criteria.GREATER_THAN));
156 return new RoleList(new ArrayList(set.getSet()));
157 }
158
159 private List getUserRoles(String blog)
160 throws DataBackendException, UnknownEntityException {
161 List l = new ArrayList();
162 Group g = TurbineSecurity.getGroupByName(blog);
163 Iterator i = TurbineSecurity.getUserList(new Criteria()).iterator();
164 for(; i.hasNext();) {
165 User u = (User)i.next();
166 AccessControlList acl = TurbineSecurity.getACL(u);
167 RoleSet rs = acl.getRoles(g);
168 for(Iterator r = rs.iterator(); r.hasNext();) {
169 l.add(new UserRole(u, (Role)r.next()));
170 }
171 }
172 return l;
173 }
174
175 protected void doBuildTemplate(RunData data, Context context)
176 throws Exception {
177 super.doBuildTemplate(data, context);
178 BlogParameterParser pp = (BlogParameterParser)data.getParameters();
179 Blog blog = pp.getBlog();
180 context.put("userGroupRoleList", getUserRoles(blog.getName()));
181 context.put("roles", getRoleList());
182 context.put("users", new UserList(
183 TurbineSecurity.getUserList(new Criteria())));
184 }
185
186 protected String[] getPermissions() {
187 return new String[]{BlogConstants.PERM_ADMIN_BLOG};
188 }
189 }