1 43 package net.jforum.view.admin; 44 45 import java.util.ArrayList ; 46 import java.util.List ; 47 48 import net.jforum.dao.CategoryDAO; 49 import net.jforum.dao.DataAccessDriver; 50 import net.jforum.dao.ForumDAO; 51 import net.jforum.dao.TopicDAO; 52 import net.jforum.dao.security.GroupSecurityDAO; 53 import net.jforum.entities.Category; 54 import net.jforum.entities.Forum; 55 import net.jforum.repository.ForumRepository; 56 import net.jforum.repository.SecurityRepository; 57 import net.jforum.repository.RolesRepository; 58 import net.jforum.security.PermissionControl; 59 import net.jforum.security.Role; 60 import net.jforum.security.RoleValue; 61 import net.jforum.security.RoleValueCollection; 62 import net.jforum.security.SecurityConstants; 63 import net.jforum.util.TreeGroup; 64 import net.jforum.util.preferences.TemplateKeys; 65 import net.jforum.view.admin.common.ModerationCommon; 66 67 71 public class ForumAction extends AdminCommand 72 { 73 public void list() throws Exception 75 { 76 this.context.put("categories", DataAccessDriver.getInstance().newCategoryDAO().selectAll()); 77 this.context.put("repository", new ForumRepository()); 78 this.setTemplateName(TemplateKeys.FORUM_ADMIN_LIST); 79 } 80 81 public void insert() throws Exception 83 { 84 CategoryDAO cm = DataAccessDriver.getInstance().newCategoryDAO(); 85 86 this.context.put("groups", new TreeGroup().getNodes()); 87 this.context.put("selectedList", new ArrayList ()); 88 this.setTemplateName(TemplateKeys.FORUM_ADMIN_INSERT); 89 this.context.put("categories",cm.selectAll()); 90 this.context.put("action", "insertSave"); 91 } 92 93 public void edit() throws Exception 95 { 96 CategoryDAO cm = DataAccessDriver.getInstance().newCategoryDAO(); 97 98 this.context.put("forum", DataAccessDriver.getInstance().newForumDAO().selectById( 99 this.request.getIntParameter("forum_id"))); 100 this.context.put("categories", cm.selectAll()); 101 this.setTemplateName(TemplateKeys.FORUM_ADMIN_EDIT); 102 this.context.put("action", "editSave"); 103 } 104 105 public void editSave() throws Exception 107 { 108 Forum f = new Forum(ForumRepository.getForum(this.request.getIntParameter("forum_id"))); 109 boolean moderated = f.isModerated(); 110 int categoryId = f.getCategoryId(); 111 112 f.setDescription(this.request.getParameter("description")); 113 f.setIdCategories(this.request.getIntParameter("categories_id")); 114 f.setName(this.request.getParameter("forum_name")); 115 f.setModerated("1".equals(this.request.getParameter("moderate"))); 116 117 DataAccessDriver.getInstance().newForumDAO().update(f); 118 119 if (moderated != f.isModerated()) { 120 new ModerationCommon().setTopicModerationStatus(f.getId(), f.isModerated()); 121 } 122 123 if (categoryId != f.getCategoryId()) { 124 f.setIdCategories(categoryId); 125 ForumRepository.removeForum(f); 126 127 f.setIdCategories(this.request.getIntParameter("categories_id")); 128 ForumRepository.addForum(f); 129 } 130 else { 131 ForumRepository.reloadForum(f.getId()); 132 } 133 134 this.list(); 135 } 136 137 public void up() throws Exception 138 { 139 this.processOrdering(true); 140 } 141 142 public void down() throws Exception 143 { 144 this.processOrdering(false); 145 } 146 147 private void processOrdering(boolean up) throws Exception 148 { 149 Forum toChange = new Forum(ForumRepository.getForum(Integer.parseInt( 150 this.request.getParameter("forum_id")))); 151 152 Category category = ForumRepository.getCategory(toChange.getCategoryId()); 153 List forums = new ArrayList (category.getForums()); 154 int index = forums.indexOf(toChange); 155 156 if (index == -1 || (up && index == 0) || (!up && index + 1 == forums.size())) { 157 this.list(); 158 return; 159 } 160 161 ForumDAO fm = DataAccessDriver.getInstance().newForumDAO(); 162 163 if (up) { 164 Forum otherForum = new Forum((Forum)forums.get(index - 1)); 166 fm.setOrderUp(toChange, otherForum); 167 } 168 else { 169 Forum otherForum = new Forum((Forum)forums.get(index + 1)); 171 fm.setOrderDown(toChange, otherForum); 172 } 173 174 category.changeForumOrder(toChange); 175 ForumRepository.refreshCategory(category); 176 177 this.list(); 178 } 179 180 public void delete() throws Exception 182 { 183 String ids[] = this.request.getParameterValues("forum_id"); 184 185 ForumDAO fm = DataAccessDriver.getInstance().newForumDAO(); 186 TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO(); 187 188 if (ids != null) { 189 for (int i = 0; i < ids.length; i++) { 190 int forumId = Integer.parseInt(ids[i]); 191 192 tm.deleteByForum(forumId); 193 fm.delete(forumId); 194 195 Forum f = new Forum(ForumRepository.getForum(forumId)); 196 ForumRepository.removeForum(f); 197 } 198 } 199 200 this.list(); 201 } 202 203 public void insertSave() throws Exception 205 { 206 Forum f = new Forum(); 207 f.setDescription(this.request.getParameter("description")); 208 f.setIdCategories(this.request.getIntParameter("categories_id")); 209 f.setName(this.request.getParameter("forum_name")); 210 f.setModerated("1".equals(this.request.getParameter("moderate"))); 211 212 int forumId = DataAccessDriver.getInstance().newForumDAO().addNew(f); 213 f.setId(forumId); 214 215 ForumRepository.addForum(f); 216 217 GroupSecurityDAO gmodel = DataAccessDriver.getInstance().newGroupSecurityDAO(); 218 PermissionControl pc = new PermissionControl(); 219 pc.setSecurityModel(gmodel); 220 221 String [] allGroups = this.request.getParameterValues("groups"); 222 223 String [] groups = this.request.getParameterValues("groupsAccess"); 225 if (groups != null) { 226 this.addRole(pc, SecurityConstants.PERM_FORUM, f.getId(), groups, false); 227 } 228 else { 229 this.addRole(pc, SecurityConstants.PERM_FORUM, f.getId(), allGroups, true); 230 } 231 232 groups = this.request.getParameterValues("groupsAnonymous"); 234 if (groups != null) { 235 this.addRole(pc, SecurityConstants.PERM_ANONYMOUS_POST, f.getId(), groups, false); 236 } 237 else { 238 this.addRole(pc, SecurityConstants.PERM_ANONYMOUS_POST, f.getId(), allGroups, true); 239 } 240 241 groups = this.request.getParameterValues("groupsReadOnly"); 243 if (groups != null) { 244 this.addRole(pc, SecurityConstants.PERM_READ_ONLY_FORUMS, f.getId(), groups, false); 245 } 246 else { 247 this.addRole(pc, SecurityConstants.PERM_READ_ONLY_FORUMS, f.getId(), allGroups, true); 248 } 249 250 this.addRole(pc, SecurityConstants.PERM_REPLY_ONLY, f.getId(), allGroups, true); 252 253 groups = this.request.getParameterValues("groupsHtml"); 255 if (groups != null) { 256 this.addRole(pc, SecurityConstants.PERM_HTML_DISABLED, f.getId(), groups, false); 257 } 258 else { 259 this.addRole(pc, SecurityConstants.PERM_HTML_DISABLED, f.getId(), allGroups, true); 260 } 261 262 SecurityRepository.clean(); 263 RolesRepository.clear(); 264 265 this.list(); 266 } 267 268 private void addRole(PermissionControl pc, String roleName, int forumId, String [] groups, boolean allow) throws Exception 269 { 270 Role role = new Role(); 271 role.setName(roleName); 272 273 for (int i = 0; i < groups.length; i++) { 274 int groupId = Integer.parseInt(groups[i]); 275 RoleValueCollection roleValues = new RoleValueCollection(); 276 277 RoleValue rv = new RoleValue(); 278 rv.setType(allow ? PermissionControl.ROLE_ALLOW : PermissionControl.ROLE_DENY); 279 rv.setValue(Integer.toString(forumId)); 280 roleValues.add(rv); 281 282 pc.addRoleValue(groupId, role, roleValues); 283 } 284 } 285 } 286 | Popular Tags |