# HG changeset patch
# User ymh
# Date 1386753785 -3600
# Node ID f8746a482459dc8cd51e958fc1278d9d532c6e78
# Parent d92a90b2ad53a4ed0d90562dd8ff8cf404d0eaee
Add first version of group
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/java/org/iri_research/renkan/controller/admin/GroupsAdminController.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/java/org/iri_research/renkan/controller/admin/GroupsAdminController.java Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,188 @@
+package org.iri_research.renkan.controller.admin;
+
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.Locale;
+
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.validation.Valid;
+
+import org.apache.commons.codec.binary.Hex;
+import org.iri_research.renkan.Constants;
+import org.iri_research.renkan.RenkanException;
+import org.iri_research.renkan.controller.Utils;
+import org.iri_research.renkan.forms.GroupForm;
+import org.iri_research.renkan.forms.GroupFormValidator;
+import org.iri_research.renkan.models.Group;
+import org.iri_research.renkan.repositories.GroupsRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.propertyeditors.StringTrimmerEditor;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort.Direction;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.client.HttpClientErrorException;
+
+@Controller
+@RequestMapping("/admin/groups")
+public class GroupsAdminController {
+
+ private final Logger logger = LoggerFactory
+ .getLogger(GroupsAdminController.class);
+
+ @Inject
+ private GroupsRepository groupsRepository;
+
+
+ @InitBinder(value = { "group" })
+ protected void initBinder(WebDataBinder binder) {
+ binder.setValidator(new GroupFormValidator());
+ }
+
+ @InitBinder
+ public void initDateBinder(final WebDataBinder dataBinder, final Locale locale) {
+ dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
+ }
+
+ @RequestMapping(value = "", method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" })
+ public String groupsList(
+ Model model,
+ @PageableDefault(sort = { "title" }, direction = Direction.DESC, page = 0, value = Constants.PAGINATION_SIZE) Pageable p,
+ HttpServletRequest request) {
+
+ Page page = this.groupsRepository.findAll(p);
+
+ model.addAttribute("page", page);
+ model.addAttribute("baseUrl", Utils.buildBaseUrl(request));
+ //TODO: add user count
+
+ return "admin/groupsList";
+ }
+
+ @RequestMapping(value = "/edit/", method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" })
+ public String editGroup(Model model) {
+ return editGroup(model, null);
+ }
+
+ @RequestMapping(value = "/edit/{groupId}", method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" })
+ public String editGroup(Model model,
+ @PathVariable(value = "groupId") String groupId) {
+
+ GroupForm groupForm = null;
+ Group group = null;
+
+ if (groupId != null && groupId.length() > 0 && !"_".equals(groupId)) {
+ group = this.groupsRepository.findOne(groupId);
+ if (group == null) {
+ throw new HttpClientErrorException(HttpStatus.NOT_FOUND,
+ "group " + groupId + " not found");
+ }
+ }
+ groupForm = new GroupForm(group);
+
+ model.addAttribute("group", groupForm);
+
+ return "admin/groupEdit";
+ }
+
+ @RequestMapping(value = "/save", method = RequestMethod.POST)
+ public String saveGroup(Model model,
+ @ModelAttribute("group") @Valid GroupForm groupForm,
+ BindingResult bindingResult) {
+
+ logger.debug("group title " + groupForm.getTitle());
+ logger.debug("user description " + groupForm.getDescription());
+
+ if (bindingResult.hasErrors()) {
+ return "admin/groupEdit";
+ }
+
+ groupForm.setGroupsRepository(groupsRepository);
+
+ try {
+ groupForm.save();
+ } catch (RenkanException e) {
+ throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "group "
+ + groupForm.getId()==null?"":groupForm.getId() + " not found");
+ }
+
+ return "redirect:/admin/groups";
+ }
+
+ @RequestMapping(value = "/delete/{groupId}")
+ public String deleteGroup(HttpServletRequest request, Model model,
+ @PathVariable(value = "groupId") String groupId,
+ @RequestParam(value = "key", required = false) String key,
+ @RequestParam(value = "salt", required = false) String salt)
+ throws NoSuchAlgorithmException, RenkanException {
+
+ if (groupId == null || groupId.length() == 0) {
+ throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
+ "Null or empty user id");
+ }
+
+ RequestMethod method = RequestMethod.valueOf(request.getMethod());
+
+ //TODO: check that group have no user
+
+
+ if (RequestMethod.GET.equals(method)) {
+
+ Group group = this.groupsRepository.findOne(groupId);
+
+ if (group == null) {
+ throw new HttpClientErrorException(HttpStatus.NOT_FOUND,
+ "group " + groupId + " not found");
+ }
+
+ SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
+ rand.setSeed(System.currentTimeMillis());
+ byte[] rawSalt = new byte[50];
+ rand.nextBytes(rawSalt);
+ String newSalt = Hex.encodeHexString(rawSalt);
+
+ model.addAttribute("groupObj", group);
+ model.addAttribute("salt", newSalt);
+ model.addAttribute("key", group.getKey(newSalt));
+
+ return "admin/groupDeleteConfirm";
+
+ } else if (RequestMethod.POST.equals(method) && key != null
+ && !key.isEmpty() && salt != null && !salt.isEmpty()) {
+
+ if (groupId != null && groupId.length() > 0) {
+
+ Group group = this.groupsRepository.findOne(groupId);
+ if (group != null) {
+ if (group.checkKey(key, salt)) {
+ this.groupsRepository.delete(groupId);
+ } else {
+ throw new HttpClientErrorException(
+ HttpStatus.BAD_REQUEST, "Key not ckecked");
+ }
+ }
+
+ }
+ return "redirect:/admin/groups";
+
+ } else {
+ throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
+ "Bad request method or parameters");
+ }
+
+ }
+
+}
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/java/org/iri_research/renkan/forms/GroupForm.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/java/org/iri_research/renkan/forms/GroupForm.java Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,66 @@
+package org.iri_research.renkan.forms;
+
+import org.iri_research.renkan.Constants;
+import org.iri_research.renkan.models.Group;
+import org.iri_research.renkan.repositories.GroupsRepository;
+import org.iri_research.renkan.repositories.IRenkanRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class GroupForm extends RenkanForm {
+
+ private String avatar;
+
+ private GroupsRepository groupsRepository;
+
+
+ public GroupForm() {
+ super();
+ }
+
+ public GroupForm(Group model) {
+ super(model);
+ if (model != null) {
+ this.avatar = model.getAvatar();
+ }
+ }
+
+ public String getAvatar() {
+ return avatar;
+ }
+
+ @Override
+ protected Group getModelInstance() {
+ return new Group();
+ }
+
+ @Override
+ protected IRenkanRepository getRepository() {
+ return this.groupsRepository;
+ }
+
+ public GroupsRepository getGroupsRepository() {
+ return groupsRepository;
+ }
+
+
+ @Override
+ protected void saveToModel() {
+ if (this.getId() == null || this.getId().length() == 0) {
+ this.model.setId(Constants.UUID_GENERATOR.generate().toString());
+ }
+ this.model.setAvatar(this.avatar);
+
+ }
+
+ public void setAvatar(String avatar) {
+ this.avatar = avatar;
+ }
+
+
+ @Autowired
+ public void setGroupsRepository(GroupsRepository groupsRepository) {
+ this.groupsRepository = groupsRepository;
+ }
+
+
+}
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/java/org/iri_research/renkan/forms/GroupFormValidator.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/java/org/iri_research/renkan/forms/GroupFormValidator.java Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,27 @@
+package org.iri_research.renkan.forms;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.Errors;
+import org.springframework.validation.ValidationUtils;
+import org.springframework.validation.Validator;
+
+@Component
+public class GroupFormValidator implements Validator {
+
+ @SuppressWarnings("unused")
+ private Logger logger = LoggerFactory.getLogger(GroupFormValidator.class);
+
+ @Override
+ public boolean supports(Class> clazz) {
+ return GroupForm.class.equals(clazz);
+ }
+
+ @Override
+ public void validate(Object target, Errors errors) {
+ ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title",
+ "renkan.error.name.empty");
+ }
+
+}
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/java/org/iri_research/renkan/models/Group.java
--- a/server/src/main/java/org/iri_research/renkan/models/Group.java Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/java/org/iri_research/renkan/models/Group.java Wed Dec 11 10:23:05 2013 +0100
@@ -5,6 +5,24 @@
@Document(collection = "groups")
public class Group extends AbstractRenkanModel {
+ private String avatar;
+
+ public Group() {
+ }
+
+ public Group(String id, String title, String description, String uri,
+ String color) {
+ super(id, title, description, uri, color);
+ }
+
+ public String getAvatar() {
+ return avatar;
+ }
+
+ public void setAvatar(String avatar) {
+ this.avatar = avatar;
+ }
+
public String getGroupName() {
return this.getTitle();
}
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/java/org/iri_research/renkan/repositories/GroupsRepository.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/java/org/iri_research/renkan/repositories/GroupsRepository.java Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,15 @@
+package org.iri_research.renkan.repositories;
+
+import java.util.List;
+
+import org.iri_research.renkan.models.Group;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+public interface GroupsRepository extends IRenkanRepository {
+
+ public List findByTitle(String title);
+
+ public Page findByTitle(String title, Pageable p);
+
+}
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/i18n/messages.properties
--- a/server/src/main/webapp/WEB-INF/i18n/messages.properties Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/i18n/messages.properties Wed Dec 11 10:23:05 2013 +0100
@@ -106,3 +106,17 @@
renkan.user.roles.ROLE_SPACES_ADMIN = Space admin
renkan.user.roles.ROLE_GROUPS_ADMIN = Groups admin
+renkanAdmin.user_objects_name = Users
+renkanAdmin.user_add = Add user
+renkanAdmin.user_edit = Edit user
+renkanAdmin.user_delete = Del. user
+renkanIndex.user_url = Url
+renkanAdmin.user_confirm_delete = Do you want to delete the user with username "{0}" ?
+
+renkanAdmin.group_objects_name = Groups
+renkanAdmin.group_add = Add group
+renkanAdmin.group_edit = Edit group
+renkanAdmin.group_delete = Del. group
+renkanIndex.group_url = Url
+renkanAdmin.group_confirm_delete = Do you want to delete the group with groupname "{0}" ?
+
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/i18n/messages_en.properties
--- a/server/src/main/webapp/WEB-INF/i18n/messages_en.properties Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/i18n/messages_en.properties Wed Dec 11 10:23:05 2013 +0100
@@ -116,3 +116,11 @@
renkan.user.roles.ROLE_SPACES_ADMIN = Space admin
renkan.user.roles.ROLE_GROUPS_ADMIN = Groups admin
+
+renkanAdmin.group_objects_name = Groups
+renkanAdmin.group_add = Add group
+renkanAdmin.group_edit = Edit group
+renkanAdmin.group_delete = Del. group
+renkanIndex.group_url = Url
+renkanAdmin.group_confirm_delete = Do you want to delete the group with groupname "{0}" ?
+
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/i18n/messages_fr.properties
--- a/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties Wed Dec 11 10:23:05 2013 +0100
@@ -113,3 +113,9 @@
renkan.user.roles.ROLE_SPACES_ADMIN = Admin. espace
renkan.user.roles.ROLE_GROUPS_ADMIN = Admin. groupes
+renkanAdmin.group_objects_name = Groupes
+renkanAdmin.group_add = aj. group
+renkanAdmin.group_edit = Edit. group
+renkanAdmin.group_delete = eff. group
+renkanIndex.group_url = Url
+renkanAdmin.group_confirm_delete = Voulez vous effacer le groupe "{0}" ?
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/adminIndex.html
--- a/server/src/main/webapp/WEB-INF/templates/admin/adminIndex.html Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/admin/adminIndex.html Wed Dec 11 10:23:05 2013 +0100
@@ -35,6 +35,9 @@
| Users |
+
+ | Groups |
+
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/groupDeleteConfirm.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/webapp/WEB-INF/templates/admin/groupDeleteConfirm.html Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,35 @@
+
+
+
+ Renkan Admin - delete group
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Do you want to Delete group with name
+
+
+
+
+
+
+
\ No newline at end of file
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/groupEdit.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/webapp/WEB-INF/templates/admin/groupEdit.html Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,39 @@
+
+
+
+ Renkan Admin - edit user
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/groupsList.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/webapp/WEB-INF/templates/admin/groupsList.html Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,68 @@
+
+
+
+ Renkan Admin - Groups
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
List of objects
+
+
+
+
+
+
+ | Groupname |
+ Edit |
+ Delete |
+
+
+
+
+ | groupname |
+ Edit |
+ Delete |
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/spaceDeleteConfirm.html
--- a/server/src/main/webapp/WEB-INF/templates/admin/spaceDeleteConfirm.html Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/admin/spaceDeleteConfirm.html Wed Dec 11 10:23:05 2013 +0100
@@ -28,7 +28,7 @@
+
+
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/spaceEdit.html
--- a/server/src/main/webapp/WEB-INF/templates/admin/spaceEdit.html Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/admin/spaceEdit.html Wed Dec 11 10:23:05 2013 +0100
@@ -31,7 +31,7 @@
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/spacesList.html
--- a/server/src/main/webapp/WEB-INF/templates/admin/spacesList.html Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/admin/spacesList.html Wed Dec 11 10:23:05 2013 +0100
@@ -67,7 +67,7 @@
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/userDeleteConfirm.html
--- a/server/src/main/webapp/WEB-INF/templates/admin/userDeleteConfirm.html Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/admin/userDeleteConfirm.html Wed Dec 11 10:23:05 2013 +0100
@@ -28,7 +28,7 @@
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/userEdit.html
--- a/server/src/main/webapp/WEB-INF/templates/admin/userEdit.html Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/admin/userEdit.html Wed Dec 11 10:23:05 2013 +0100
@@ -32,7 +32,7 @@
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/admin/usersList.html
--- a/server/src/main/webapp/WEB-INF/templates/admin/usersList.html Mon Dec 09 10:23:30 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/admin/usersList.html Wed Dec 11 10:23:05 2013 +0100
@@ -63,7 +63,7 @@
diff -r d92a90b2ad53 -r f8746a482459 server/src/main/webapp/WEB-INF/templates/fragment/groupForm.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/webapp/WEB-INF/templates/fragment/groupForm.html Wed Dec 11 10:23:05 2013 +0100
@@ -0,0 +1,77 @@
+
+
+