1 53 54 package com.Yasna.forum.tags; 55 56 import javax.servlet.jsp.tagext.*; 57 import com.Yasna.forum.*; 58 import javax.servlet.jsp.*; 59 import java.net.*; 60 import java.util.*; 61 62 68 public class PerformAdminActionTag extends TagSupport { 69 70 private Action target; 71 72 public int doStartTag() throws JspException { 73 try { 74 Authorization authToken = getAuthToken(); 75 ForumFactory forumFactory = ForumFactory.getInstance(authToken); 76 ForumPermissions permissions = forumFactory.getPermissions(authToken); 77 boolean isSystemAdmin = permissions.get(ForumPermissions.SYSTEM_ADMIN); 78 boolean isUserAdmin = permissions.get(ForumPermissions.FORUM_ADMIN); 79 boolean isModerator = false; 80 Forum forum = getCurrentForum(); 81 if (forum != null) { 82 isModerator = forum.getPermissions(authToken).get(ForumPermissions.MODERATOR); 83 } 84 boolean isAdmin = isUserAdmin || isSystemAdmin || isModerator; 85 if (isAdmin) { 86 Enumeration en = pageContext.getRequest().getParameterNames(); 87 while (en.hasMoreElements()) { 88 String p = (String ) en.nextElement(); 89 if (p.startsWith("approve_")) { 90 try { 91 String idStr = p.substring(8, p.indexOf(".")); 92 int id = Integer.parseInt(idStr); 93 target.setApprovement(id, true); 94 break; 95 } catch(Exception e) { 96 e.printStackTrace(); 97 } 98 } else if (p.startsWith("delete_")) { 99 try { 100 String idStr = p.substring(7, p.indexOf(".")); 101 int id = Integer.parseInt(idStr); 102 target.delete(id); 103 break; 104 } catch(Exception e) { 105 e.printStackTrace(); 106 } 107 } 108 } 109 110 } } catch(Exception e) { 112 } 113 return SKIP_BODY; 114 } 115 116 121 public void setTarget(String t) throws Exception { 122 if ("thread".equals(t)) { 123 target = new TargetThread(); 124 } else if ("forum".equals(t)) { 125 target = new TargetForum(); 126 } else { 127 throw new Exception ("Unknown 'target' = [" + target + "] set for attribute on PerformAdminAction tag!"); 128 } 129 } 130 131 137 private Authorization getAuthToken() { 138 YazdState js = (YazdState) pageContext.getAttribute("yazdUserState",PageContext.SESSION_SCOPE); 139 return js.getAuthorization(); 140 } 141 142 private ForumThread getCurrentThread() { 143 ThreadTag tt = null; 144 try { 145 tt = (ThreadTag)this.findAncestorWithClass(this, 146 Class.forName("com.Yasna.forum.tags.ThreadTag")); 147 } catch(Exception e) { 148 } 149 return tt.getThread(); 150 } 151 152 private Forum getCurrentForum() { 153 ForumTag tt = null; 154 try { 155 tt = (ForumTag)this.findAncestorWithClass(this, 156 Class.forName("com.Yasna.forum.tags.ForumTag")); 157 } catch(Exception e) { 158 } 159 return tt.getForum(); 160 } 161 162 interface Action { 163 public void delete(int id) throws Exception ; 164 public void setApprovement(int id, boolean approvement) throws Exception ; 165 } 166 class TargetThread implements Action { 167 public void delete(int id) throws Exception { 168 ForumThread thread = getCurrentThread(); 169 ForumMessage toDelete = thread.getMessage(id); 170 thread.deleteMessage(toDelete); 171 } 172 public void setApprovement(int id, boolean approvement) throws Exception { 173 ForumThread thread = getCurrentThread(); 174 thread.getMessage(id).setApprovment(approvement); 175 } 176 } 177 class TargetForum implements Action { 178 public void delete(int id) throws Exception { 179 Forum forum = getCurrentForum(); 180 ForumThread toDelete = forum.getThread(id); 181 forum.deleteThread(toDelete); 182 } 183 public void setApprovement(int id, boolean approvement) throws Exception { 184 Forum forum = getCurrentForum(); 185 forum.getThread(id).setApprovment(approvement); 186 } 187 } 188 189 } | Popular Tags |