1 19 package org.lucane.applications.forumadmin; 20 21 import org.lucane.common.*; 22 import org.lucane.common.acl.AclInfo; 23 import org.lucane.common.acl.AclProducer; 24 import org.lucane.common.concepts.GroupConcept; 25 import org.lucane.common.concepts.UserConcept; 26 import org.lucane.common.net.ObjectConnection; 27 import org.lucane.server.*; 28 import org.lucane.server.acl.AccessController; 29 import java.sql.*; 30 import java.util.*; 31 32 33 public class ForumAdminService 34 extends Service 35 implements AclProducer 36 { 37 private static final String FORUM_APP = "org.lucane.applications.forum"; 38 39 public ForumAdminService() 40 { 41 } 42 43 public void process(ObjectConnection oc, Message message) 44 { 45 ForumAdminAction faa = (ForumAdminAction)message.getData(); 46 47 try { 48 switch(faa.action) 49 { 50 case ForumAdminAction.LIST_FORUMS: 51 ArrayList forums = listForums(); 52 oc.write("OK"); 53 oc.write(forums); 54 break; 55 56 case ForumAdminAction.DELETE_FORUM: 57 deleteForum(faa.forum); 58 oc.write("OK"); 59 break; 60 61 case ForumAdminAction.CREATE_FORUM: 62 createForum(faa.forum); 63 oc.write("OK"); 64 break; 65 66 case ForumAdminAction.GET_ACLS: 67 AclInfo[] acl = getAcls(faa.forum); 68 oc.write("OK"); 69 oc.write(acl); 70 break; 71 72 case ForumAdminAction.GET_GROUPS: 73 ArrayList groups = getAllGroups(); 74 oc.write("OK"); 75 oc.write(groups); 76 break; 77 78 case ForumAdminAction.GET_USERS: 79 ArrayList users = getAllUsers(); 80 oc.write("OK"); 81 oc.write(users); 82 break; 83 84 case ForumAdminAction.ADD_ACL: 85 addAcl(faa.forum, (AclInfo)faa.param); 86 oc.write("OK"); 87 break; 88 89 case ForumAdminAction.REMOVE_ACL: 90 removeAcl(faa.forum, (AclInfo)faa.param); 91 oc.write("OK"); 92 break; 93 } 94 } catch(Exception e) { 95 try { 96 oc.write("FAILED " + e); 97 } catch(Exception e2) {} 98 99 e.printStackTrace(); 100 } 101 } 102 103 public ArrayList listForums() 104 throws Exception 105 { 106 ArrayList forums = new ArrayList(); 107 108 Connection connection = Server.getInstance().getDBLayer().getConnection(); 109 PreparedStatement select = connection.prepareStatement("SELECT name FROM forum"); 110 ResultSet res = select.executeQuery(); 111 112 while(res.next()) 113 forums.add(res.getString(1)); 114 115 res.close(); 116 select.close(); 117 connection.close(); 118 119 return forums; 120 } 121 122 public void deleteForum(String forum) 123 throws Exception 124 { 125 Connection connection = Server.getInstance().getDBLayer().getConnection(); 126 PreparedStatement delete = connection.prepareStatement("DELETE FROM forum WHERE name= ?"); 127 delete.setString(1, forum); 128 delete.execute(); 129 delete.close(); 130 connection.close(); 131 132 Server.getInstance().getAccessController().removeItem(FORUM_APP, forum); 134 } 135 136 public void createForum(String forum) 137 throws Exception 138 { 139 Connection connection = Server.getInstance().getDBLayer().getConnection(); 140 PreparedStatement insert = connection.prepareStatement("INSERT INTO forum VALUES(?)"); 141 insert.setString(1, forum); 142 insert.execute(); 143 insert.close(); 144 connection.close(); 145 146 Server.getInstance().getAccessController().allowGroup(FORUM_APP, forum, "Moderate", "Admins"); 148 Server.getInstance().getAccessController().allowGroup(FORUM_APP, forum, "Write", "AllUsers"); 149 Server.getInstance().getAccessController().allowGroup(FORUM_APP, forum, "Read", "AllUsers"); 150 } 151 152 public AclInfo[] getAcls(String forum) 153 throws Exception 154 { 155 AccessController acl = Server.getInstance().getAccessController(); 156 return acl.getAcls(FORUM_APP, forum); 157 } 158 159 public ArrayList getAllGroups() 160 throws Exception 161 { 162 ArrayList groups = new ArrayList(); 163 Iterator i = Server.getInstance().getStore().getGroupStore().getAllGroups(); 164 while(i.hasNext()) 165 { 166 GroupConcept concept = (GroupConcept)i.next(); 167 groups.add(concept.getName()); 168 } 169 170 return groups; 171 } 172 173 public ArrayList getAllUsers() 174 throws Exception 175 { 176 ArrayList users = new ArrayList(); 177 Iterator i = Server.getInstance().getStore().getUserStore().getAllUsers(); 178 while(i.hasNext()) 179 { 180 UserConcept concept = (UserConcept)i.next(); 181 users.add(concept.getName()); 182 } 183 184 return users; 185 } 186 187 public void addAcl(String forum, AclInfo aclInfo) 188 throws Exception 189 { 190 AccessController acl = Server.getInstance().getAccessController(); 191 if(aclInfo.getGroup() != null) 192 { 193 if(aclInfo.isAllow()) 194 acl.allowGroup(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getGroup()); 195 else 196 acl.denyGroup(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getGroup()); 197 } 198 else if(aclInfo.getUser() != null) 199 { 200 if(aclInfo.isAllow()) 201 acl.allowUser(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getUser()); 202 else 203 acl.denyUser(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getUser()); 204 } 205 } 206 207 public void removeAcl(String forum, AclInfo aclInfo) 208 throws Exception 209 { 210 AccessController acl = Server.getInstance().getAccessController(); 211 if(aclInfo.getGroup() != null) 212 acl.removeAclForGroup(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getGroup()); 213 else if(aclInfo.getUser() != null) 214 acl.removeAclForUser(FORUM_APP, forum, aclInfo.getAccess(), aclInfo.getUser()); 215 } 216 217 public String [] getAccesses() 219 { 220 return new String [] {"Moderate", "Write", "Read"}; 221 } 222 223 public String getItemName(String itemId) 224 { 225 return itemId; 226 } 227 } 228 | Popular Tags |