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.StoredBlog;
30 import net.sourceforge.blogentis.om.StoredBlogPeer;
31 import net.sourceforge.blogentis.turbine.BaseBlogScreen;
32 import net.sourceforge.blogentis.turbine.BlogParameterParser;
33
34 import org.apache.turbine.om.security.Group;
35 import org.apache.turbine.om.security.Role;
36 import org.apache.turbine.om.security.User;
37 import org.apache.turbine.services.security.TurbineSecurity;
38 import org.apache.turbine.util.RunData;
39 import org.apache.turbine.util.security.AccessControlList;
40 import org.apache.turbine.util.security.DataBackendException;
41 import org.apache.turbine.util.security.RoleSet;
42 import org.apache.turbine.util.security.UnknownEntityException;
43 import org.apache.velocity.context.Context;
44
45 /***
46 * @author abas
47 */
48 public class UserIndex
49 extends BaseBlogScreen {
50 public static class BlogRole {
51 public StoredBlog blog;
52
53 public Role role;
54
55 BlogRole(StoredBlog b, Role role) {
56 this.blog = b;
57 this.role = role;
58 }
59
60 public Role getRole() {
61 return role;
62 }
63
64 public StoredBlog getBlog() {
65 return blog;
66 }
67 }
68
69 protected void doBuildTemplate(RunData data, Context context)
70 throws Exception {
71 super.doBuildTemplate(data, context);
72 User currentUser = data.getUser();
73 BlogParameterParser pp = (BlogParameterParser)data.getParameters();
74 String userName = currentUser.getName();
75 userName = pp.getString("user", currentUser.getName());
76 User shownUser = TurbineSecurity.getUser(userName);
77 int state = 0;
78 if (currentUser.hasLoggedIn()
79 && (currentUser.getName().equals(shownUser.getName()) || data
80 .getACL().hasPermission("admin_site"))) {
81 context.put("sameUser", Boolean.TRUE);
82 state = pp.getInt("s", 0);
83 }
84 switch (state) {
85 case 1:
86 doBuildUserInfo(data, context, shownUser);
87 break;
88 case 3:
89 duBuldUserPreferences(data, context, shownUser);
90 break;
91 }
92
93 context.put("user", shownUser);
94 context.put("userFullName", shownUser.getFirstName() + " "
95 + shownUser.getLastName());
96 context.put("state", new Integer(state));
97 }
98
99 /***
100 * @param data
101 * @param context
102 * @param shownUser
103 */
104 private void duBuldUserPreferences(RunData data, Context context,
105 User shownUser) {}
106
107 private List getUserRoles(User u)
108 throws DataBackendException, UnknownEntityException {
109 AccessControlList acl = TurbineSecurity.getACL(u);
110 List l = new ArrayList();
111 Iterator i = TurbineSecurity.getAllGroups().iterator();
112 for(; i.hasNext();) {
113 Group g = (Group)i.next();
114 try {
115 StoredBlog b = StoredBlogPeer.retrieveByName(g.getName());
116 RoleSet rs = acl.getRoles(g);
117 for(Iterator r = rs.iterator(); r.hasNext();) {
118 l.add(new BlogRole(b, (Role)r.next()));
119 }
120 } catch (Exception e) {}
121 }
122 return l;
123 }
124
125 /***
126 * @param data
127 * @param context
128 * @param shownUser
129 */
130 private void doBuildUserInfo(RunData data, Context context, User shownUser)
131 throws DataBackendException, UnknownEntityException {
132 context.put("blogRoleList", getUserRoles(shownUser));
133 }
134
135 protected boolean isAuthorized(RunData data, Context context) {
136 BlogParameterParser pp = (BlogParameterParser)data.getParameters();
137 String userName = pp.getString("user", data.getUser().getName());
138 if (userName == null)
139 return false;
140 try {
141 TurbineSecurity.getUser(userName);
142 } catch (DataBackendException e) {
143 return false;
144 } catch (UnknownEntityException e) {
145 return false;
146 }
147 return true;
148 }
149 }