1 31 package org.blojsom.plugin.admin; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Entry; 37 import org.blojsom.blog.User; 38 import org.blojsom.fetcher.Fetcher; 39 import org.blojsom.fetcher.FetcherException; 40 import org.blojsom.plugin.PluginException; 41 import org.blojsom.util.BlojsomConstants; 42 import org.blojsom.util.BlojsomUtils; 43 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.http.HttpServletResponse ; 46 import java.io.File ; 47 import java.io.IOException ; 48 import java.util.Date ; 49 import java.util.HashMap ; 50 import java.util.Map ; 51 import java.util.Properties ; 52 53 60 public class EditBlogsPlugin extends BaseAdminPlugin { 61 62 private Log _logger = LogFactory.getLog(EditBlogsPlugin.class); 63 64 private static final String EDIT_BLOG_USERS_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blogs"; 66 67 private static final String FAILED_DELETE_BLOGS_PERMISSION_KEY = "failed.delete.blogs.permission.text"; 69 private static final String FAILED_REMOVE_BLOG_CONFIGURATION_KEY = "failed.remove.blog.configuration.text"; 70 private static final String FAILED_REMOVE_BLOG_DIRECTORY_KEY = "failed.remove.blog.directory.text"; 71 private static final String FAILED_REMOVE_BLOG_RESOURCES_DIRECTORY_KEY = "failed.remove.blog.resources.text"; 72 private static final String DELETED_BLOG_KEY = "deleted.blog.text"; 73 74 private static final String FAILED_DELETE_PROTECTED_BLOG_KEY = "failed.delete.protected.blog.text"; 75 private static final String FAILED_ADD_BLOGS_PERMISSION_KEY = "failed.add.blogs.permission.text"; 76 private static final String MISSING_WEBLOG_ID_KEY = "missing.weblog.id.text"; 77 private static final String WEBLOG_ID_EXISTS_KEY = "weblog.id.exists.text"; 78 private static final String WEBLOG_DIRECTORY_EXISTS_KEY = "weblog.directory.exists.text"; 79 private static final String PASSWORDS_NOT_MATCHED_KEY = "passwords.not.matched.text"; 80 private static final String FAILED_BOOTSTRAP_DIRECTORY_COPY_KEY = "failed.bootstrap.directory.copy.text"; 81 private static final String FAILED_BLOG_DIRECTORY_CREATE_KEY = "failed.blog.directory.create.text"; 82 private static final String ADDED_NEW_WEBLOG_KEY = "added.new.blog.text"; 83 84 private static final String BLOJSOM_PLUGIN_EDIT_BLOGS = "BLOJSOM_PLUGIN_EDIT_BLOGS"; 85 86 private static final String DELETE_BLOG_USER_ACTION = "delete-blog-user"; 88 private static final String ADD_BLOG_USER_ACTION = "add-blog-user"; 89 90 private static final String BLOG_ID = "blog-id"; 92 private static final String BLOG_LOGIN_ID = "blog-login-id"; 93 private static final String BLOG_USER_EMAIL = "blog-user-email"; 94 private static final String BLOG_USER_NAME = "blog-user-name"; 95 private static final String BLOG_USER_PASSWORD = "blog-user-password"; 96 private static final String BLOG_USER_PASSWORD_CHECK = "blog-user-password-check"; 97 98 private static final String ADD_BLOG_PERMISSION = "add_blog"; 100 private static final String DELETE_BLOG_PERMISSION = "delete_blog"; 101 102 private Fetcher _fetcher; 103 private Map _defaultBlogProperties; 104 private Map _defaultTemplateProperties; 105 private Map _defaultPluginProperties; 106 private Properties _blojsomProperties; 107 private Map _protectedBlogs; 108 109 112 public EditBlogsPlugin() { 113 } 114 115 120 public void setFetcher(Fetcher fetcher) { 121 _fetcher = fetcher; 122 } 123 124 129 public void setDefaultBlogProperties(Map defaultBlogProperties) { 130 _defaultBlogProperties = defaultBlogProperties; 131 } 132 133 138 public void setDefaultTemplateProperties(Map defaultTemplateProperties) { 139 _defaultTemplateProperties = defaultTemplateProperties; 140 } 141 142 147 public void setDefaultPluginProperties(Map defaultPluginProperties) { 148 _defaultPluginProperties = defaultPluginProperties; 149 } 150 151 156 public void setBlojsomProperties(Properties blojsomProperties) { 157 _blojsomProperties = blojsomProperties; 158 } 159 160 165 public void setProtectedBlogs(Map protectedBlogs) { 166 _protectedBlogs = protectedBlogs; 167 } 168 169 180 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 181 if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) { 182 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE); 183 184 return entries; 185 } 186 187 String username = getUsernameFromSession(httpServletRequest, blog); 188 189 try { 190 context.put(BLOJSOM_PLUGIN_EDIT_BLOGS, _fetcher.loadBlogIDs()); 191 } catch (FetcherException e) { 192 _logger.error(e); 193 } 194 195 String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 196 if (BlojsomUtils.checkNullOrBlank(action)) { 197 if (_logger.isDebugEnabled()) { 198 _logger.debug("User did not request edit action"); 199 } 200 201 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 202 } else if (PAGE_ACTION.equals(action)) { 203 if (_logger.isDebugEnabled()) { 204 _logger.debug("User requested edit blogs page"); 205 } 206 207 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 208 } else if (DELETE_BLOG_USER_ACTION.equals(action)) { 209 if (_logger.isDebugEnabled()) { 210 _logger.debug("User requested delete blog action"); 211 } 212 213 if (!checkPermission(blog, null, username, DELETE_BLOG_PERMISSION)) { 215 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 216 addOperationResultMessage(context, getAdminResource(FAILED_DELETE_BLOGS_PERMISSION_KEY, FAILED_DELETE_BLOGS_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 217 218 return entries; 219 } 220 221 String blogID = BlojsomUtils.getRequestValue(BLOG_ID, httpServletRequest); 222 223 if (BlojsomUtils.checkNullOrBlank(blogID)) { 224 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 225 return entries; 226 } else if (_protectedBlogs.containsKey(blogID)) { 227 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 228 addOperationResultMessage(context, formatAdminResource(FAILED_DELETE_PROTECTED_BLOG_KEY, FAILED_DELETE_PROTECTED_BLOG_KEY, blog.getBlogAdministrationLocale(), new Object [] {blogID})); 229 230 return entries; 231 } else { 232 if (_logger.isDebugEnabled()) { 233 _logger.debug("Deleting blog: " + blogID); 234 } 235 236 try { 237 Blog blogToDelete = _fetcher.loadBlog(blogID); 238 _fetcher.deleteBlog(blogToDelete); 239 } catch (FetcherException e) { 240 if (_logger.isErrorEnabled()) { 241 _logger.error(e); 242 } 243 244 addOperationResultMessage(context, formatAdminResource(FAILED_REMOVE_BLOG_CONFIGURATION_KEY, FAILED_REMOVE_BLOG_CONFIGURATION_KEY, blog.getBlogAdministrationLocale(), new Object []{blogID})); 245 246 return entries; 247 } 248 249 String blogsDirectoryPath = _blojsomProperties.getProperty(BlojsomConstants.BLOGS_DIRECTORY_IP, BlojsomConstants.DEFAULT_BLOGS_DIRECTORY); 250 String resourcesDirectoryPath = _blojsomProperties.getProperty(BlojsomConstants.RESOURCES_DIRECTORY_IP, BlojsomConstants.DEFAULT_RESOURCES_DIRECTORY); 251 252 File blogDirectory = new File (_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY) + blogsDirectoryPath + blogID + "/"); 253 if (!BlojsomUtils.deleteDirectory(blogDirectory)) { 254 if (_logger.isErrorEnabled()) { 255 _logger.error("Unable to remove blog directory: " + blogDirectory.toString()); 256 } 257 258 addOperationResultMessage(context, formatAdminResource(FAILED_REMOVE_BLOG_DIRECTORY_KEY, FAILED_REMOVE_BLOG_DIRECTORY_KEY, blog.getBlogAdministrationLocale(), new Object []{blogID})); 259 } else { 260 if (_logger.isDebugEnabled()) { 261 _logger.debug("Removed blog directory: " + blogDirectory.toString()); 262 } 263 } 264 265 File blogResourcesDirectory = new File (_servletConfig.getServletContext().getRealPath("/") + resourcesDirectoryPath + blogID + "/"); 266 if (!BlojsomUtils.deleteDirectory(blogResourcesDirectory)) { 267 if (_logger.isErrorEnabled()) { 268 _logger.error("Unable to remove blog resource directory: " + blogResourcesDirectory.toString()); 269 } 270 271 addOperationResultMessage(context, formatAdminResource(FAILED_REMOVE_BLOG_RESOURCES_DIRECTORY_KEY, FAILED_REMOVE_BLOG_RESOURCES_DIRECTORY_KEY, blog.getBlogAdministrationLocale(), new Object []{blogID})); 272 } else { 273 if (_logger.isDebugEnabled()) { 274 _logger.debug("Removed blog resource directory: " + blogResourcesDirectory.toString()); 275 } 276 } 277 278 if (_logger.isDebugEnabled()) { 279 _logger.debug("Wrote new blojsom configuration after deleting blog: " + blogID); 280 } 281 282 try { 283 context.put(BLOJSOM_PLUGIN_EDIT_BLOGS, _fetcher.loadBlogIDs()); 284 } catch (FetcherException e) { 285 _logger.error(e); 286 } 287 288 addOperationResultMessage(context, formatAdminResource(DELETED_BLOG_KEY, DELETED_BLOG_KEY, blog.getBlogAdministrationLocale(), new Object []{blogID})); 289 } 290 291 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 292 } else if (ADD_BLOG_USER_ACTION.equals(action)) { 293 if (_logger.isDebugEnabled()) { 294 _logger.debug("User requested add blog action"); 295 } 296 297 if (!checkPermission(blog, null, username, ADD_BLOG_PERMISSION)) { 299 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 300 addOperationResultMessage(context, getAdminResource(FAILED_ADD_BLOGS_PERMISSION_KEY, FAILED_ADD_BLOGS_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 301 302 return entries; 303 } 304 305 String blogID = BlojsomUtils.getRequestValue(BLOG_ID, httpServletRequest); 306 307 if (BlojsomUtils.checkNullOrBlank(blogID)) { addOperationResultMessage(context, getAdminResource(MISSING_WEBLOG_ID_KEY, MISSING_WEBLOG_ID_KEY, blog.getBlogAdministrationLocale())); 309 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 310 311 return entries; 312 } else { if (_logger.isDebugEnabled()) { 314 _logger.debug("Adding new blog id: " + blogID); 315 } 316 317 try { 318 _fetcher.loadBlog(blogID); 319 320 addOperationResultMessage(context, formatAdminResource(WEBLOG_ID_EXISTS_KEY, WEBLOG_ID_EXISTS_KEY, blog.getBlogAdministrationLocale(), new Object []{blogID})); 321 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 322 323 return entries; 324 } catch (FetcherException e) { 325 } 326 327 Blog blogToAdd = _fetcher.newBlog(); 328 blogToAdd.setBlogId(blogID); 329 330 File blogDirectory = new File (_servletConfig.getServletContext().getRealPath("/") + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + blogID); 331 if (blogDirectory.exists()) 332 { if (_logger.isDebugEnabled()) { 334 _logger.debug("Blog directory already exists for blog ID: " + blogID); 335 } 336 337 addOperationResultMessage(context, formatAdminResource(WEBLOG_DIRECTORY_EXISTS_KEY, WEBLOG_DIRECTORY_EXISTS_KEY, blog.getBlogAdministrationLocale(), new Object []{blogID})); 338 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 339 340 return entries; 341 } else { String blogUserPassword = BlojsomUtils.getRequestValue(BLOG_USER_PASSWORD, httpServletRequest); 343 String blogUserPasswordCheck = BlojsomUtils.getRequestValue(BLOG_USER_PASSWORD_CHECK, httpServletRequest); 344 345 if (!blogUserPassword.equals(blogUserPasswordCheck)) { 347 if (_logger.isDebugEnabled()) { 348 _logger.debug("User password does not equal password check"); 349 } 350 351 addOperationResultMessage(context, getAdminResource(PASSWORDS_NOT_MATCHED_KEY, PASSWORDS_NOT_MATCHED_KEY, blog.getBlogAdministrationLocale())); 352 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 353 354 return entries; 355 } else { blogToAdd.setProperties(_defaultBlogProperties); 358 blogToAdd.setTemplates(_defaultTemplateProperties); 359 blogToAdd.setPlugins(_defaultPluginProperties); 360 361 BlojsomUtils.resolveDynamicBaseAndBlogURL(httpServletRequest, blogToAdd, blogID); 362 363 try { 364 _fetcher.saveBlog(blogToAdd); 365 } catch (FetcherException e) { 366 if (_logger.isErrorEnabled()) { 367 _logger.error(e); 368 } 369 370 return entries; 371 } 372 373 String blogLoginID = BlojsomUtils.getRequestValue(BLOG_LOGIN_ID, httpServletRequest); 374 String blogUserEmail = BlojsomUtils.getRequestValue(BLOG_USER_EMAIL, httpServletRequest); 375 String blogUserName = BlojsomUtils.getRequestValue(BLOG_USER_NAME, httpServletRequest); 376 377 if (!BlojsomUtils.checkNullOrBlank(blogLoginID) && !BlojsomUtils.checkNullOrBlank(blogUserEmail)) { 378 User user = _fetcher.newUser(); 379 user.setBlogId(blogToAdd.getId()); 380 user.setUserEmail(blogUserEmail); 381 Map userMetaData = new HashMap (); 382 userMetaData.put("all_permissions_permission", "true"); 383 user.setMetaData(userMetaData); 384 user.setUserLogin(blogLoginID); 385 if (!BlojsomUtils.checkNullOrBlank(blogUserName)) { 386 user.setUserName(blogUserName); 387 } else { 388 user.setUserName(blogLoginID); 389 } 390 user.setUserPassword(blogUserPassword); 391 user.setUserRegistered(new Date ()); 392 user.setUserStatus("new"); 393 394 try { 395 _fetcher.saveUser(blogToAdd, user); 396 } catch (FetcherException e) { 397 if (_logger.isErrorEnabled()) { 398 _logger.error(e); 399 } 400 } 401 402 String blogsDirectoryPath = _blojsomProperties.getProperty(BlojsomConstants.BLOGS_DIRECTORY_IP, BlojsomConstants.DEFAULT_BLOGS_DIRECTORY); 403 String bootstrapDirectoryPath = _blojsomProperties.getProperty(BlojsomConstants.BOOTSTRAP_DIRECTORY_IP, BlojsomConstants.DEFAULT_BOOTSTRAP_DIRECTORY); 404 String templatesDirectoryPath = _blojsomProperties.getProperty(BlojsomConstants.TEMPLATES_DIRECTORY_IP, BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY); 405 String resourcesDirectoryPath = _blojsomProperties.getProperty(BlojsomConstants.RESOURCES_DIRECTORY_IP, BlojsomConstants.DEFAULT_RESOURCES_DIRECTORY); 406 407 File bootstrapResourcesDirectory = new File (_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY) + blogsDirectoryPath + bootstrapDirectoryPath + resourcesDirectoryPath); 408 File newBlogResourcesDirectory = new File (_servletConfig.getServletContext().getRealPath(resourcesDirectoryPath) + "/" + blogID); 409 File bootstrapTemplatesDirectory = new File (_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY) + blogsDirectoryPath + bootstrapDirectoryPath + templatesDirectoryPath); 410 File newBlogTemplatesDirectory = new File (_servletConfig.getServletContext().getRealPath(BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY) + blogsDirectoryPath + blogID + templatesDirectoryPath); 411 412 try { 413 newBlogResourcesDirectory.mkdirs(); 414 BlojsomUtils.copyDirectory(bootstrapResourcesDirectory, newBlogResourcesDirectory); 415 } catch (IOException e) { 416 if (_logger.isErrorEnabled()) { 417 _logger.error(e); 418 } 419 420 addOperationResultMessage(context, getAdminResource(FAILED_BOOTSTRAP_DIRECTORY_COPY_KEY, FAILED_BOOTSTRAP_DIRECTORY_COPY_KEY, blog.getBlogAdministrationLocale())); 421 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 422 423 return entries; 424 } 425 426 try { 427 newBlogTemplatesDirectory.mkdirs(); 428 BlojsomUtils.copyDirectory(bootstrapTemplatesDirectory, newBlogTemplatesDirectory); 429 } catch (IOException e) { 430 if (_logger.isErrorEnabled()) { 431 _logger.error(e); 432 } 433 434 addOperationResultMessage(context, getAdminResource(FAILED_BLOG_DIRECTORY_CREATE_KEY, FAILED_BLOG_DIRECTORY_CREATE_KEY, blog.getBlogAdministrationLocale())); 435 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 436 437 return entries; 438 } 439 440 try { 441 context.put(BLOJSOM_PLUGIN_EDIT_BLOGS, _fetcher.loadBlogIDs()); 442 } catch (FetcherException e) { 443 _logger.error(e); 444 } 445 446 addOperationResultMessage(context, formatAdminResource(ADDED_NEW_WEBLOG_KEY, ADDED_NEW_WEBLOG_KEY, blog.getBlogAdministrationLocale(), new Object []{blogID})); 447 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_USERS_PAGE); 448 } else { 449 } 451 } 452 } 453 } 454 } 455 456 return entries; 457 } 458 } 459 | Popular Tags |