Commit 57660493 authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

No commit message

No commit message
parent 63990db9
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>dariah-samlsp-orm</name>
<comment>This project is a simple template for a jar utility using Spring.
</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.2.7.200909012107-CI-R3397-B453]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[true]]></enableImports>
<configs>
</configs>
<configSets>
<configSet>
<name><![CDATA[test]]></name>
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
</configs>
</configSet>
</configSets>
</beansProjectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>de.dariah</groupId>
<artifactId>dariah-saml-orm</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DARIAH SAML Persistence Components</name>
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java-version>1.7</java-version>
<org.springframework-version>3.1.2.RELEASE</org.springframework-version>
</properties>
<repositories>
<repository>
<id>dariah</id>
<name>DARIAH MInf internal repo</name>
<url>http://141.13.17.66:8080/archiva/repository/dariah/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- DARIAH Base Dependencies -->
<dependency>
<artifactId>dariah-base</artifactId>
<groupId>de.dariah</groupId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<artifactId>dariah-javasp-core</artifactId>
<groupId>de.dariah</groupId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<artifactId>dariah-javasp-web</artifactId>
<groupId>de.dariah</groupId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<artifactId>spring-security-saml2-core</artifactId>
<groupId>de.dariah</groupId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
</project>
package de.dariah.samlsp.controller;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;
import javax.validation.Valid;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import de.dariah.aai.web.controller.ExceptionHandlingController;
import de.dariah.samlsp.orm.model.RoleImpl;
import de.dariah.samlsp.orm.model.RoleMapping;
import de.dariah.samlsp.orm.service.RoleService;
import de.dariah.samlsp.orm.service.UserService;
@Controller
@RequestMapping("/admin/auth")
public class AuthController extends ExceptionHandlingController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@RequestMapping(value = "/ajax/delete", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
public @ResponseBody String deleteMapping(@RequestParam int id) {
//logger.debug(String.format("Command received to delete role mapping [%s]", id));
roleService.removeMapping(id);
GsonBuilder bldr = new GsonBuilder();
Gson gson = bldr.create();
JsonObject result = new JsonObject();
result.addProperty("success", true);
result.addProperty("message_type", "success");
result.addProperty("message_head", "Deleted role mapping!");
result.addProperty("message_body", "Successfully deleted role mapping");
return gson.toJson(result);
}
@RequestMapping(method = RequestMethod.GET, value = "/new")
public String getNewForm(Model model, Locale locale) {
return getEditForm(0, model, locale);
}
@RequestMapping(method = RequestMethod.GET, value = "/edit")
public String getEditForm(@RequestParam int id, Model model, Locale locale) {
RoleMapping rm = null;
if (id > 0) {
rm = roleService.findRoleMapping(id);
rm.setRoleId(rm.getRole().getId());
} else {
rm = new RoleMapping();
rm.setActive(true);
}
model.addAttribute("roleMapping", rm);
List<String> knownEndpoints = userService.getKnownEndpoints();
List<RoleImpl> roles = new ArrayList<RoleImpl>();
for(RoleImpl r : userService.getAllRoles()) {
/*if (r.getId()!=ROLES.ROLE_AUTHENTICATED_GUEST.getId()) {*/
roles.add(r);
/*}*/
}
model.addAttribute("knownEndpoints", knownEndpoints);
model.addAttribute("roles", roles);
model.addAttribute("formattedDate", DateTimeFormat.forPattern(DateTimeFormat.patternForStyle("SS", locale)).print(DateTime.now()));
return "admin/auth/management/edit";
}
@RequestMapping(method=POST, value="/ajax/save", produces = "application/json; charset=utf-8")
public @ResponseBody String saveRoleMapping(@Valid RoleMapping roleMapping, BindingResult bindingResult, Model model, Locale locale) throws Exception {
GsonBuilder bldr = new GsonBuilder();
Gson gson = bldr.create();
JsonObject result = new JsonObject();
result.addProperty("success", !bindingResult.hasErrors());
result.addProperty("errorCount", bindingResult.getErrorCount());
if (bindingResult.hasErrors()) {
//result.add("errors", getJsonErrorList(bindingResult, locale));
} else {
roleService.createOrUpdate(roleMapping);
result.addProperty("id", roleMapping.getId());
}
return gson.toJson(result);
}
@RequestMapping(value = {"/", ""}, method = RequestMethod.GET)
public String getRoleManagement(@RequestParam(defaultValue="1") Integer selectedRole, @RequestParam(defaultValue="false") Boolean manageUsers,
@RequestParam(defaultValue="0") Integer assignmentFilter, Model model, Locale locale) {
List<RoleImpl> roles = userService.getAllRoles();
List<RoleMapping> roleMappings = roleService.getAllRoleMappings();
TreeMap<RoleImpl, List<RoleMapping>> hashedMappings = new TreeMap<RoleImpl, List<RoleMapping>>(new Comparator<RoleImpl>() {
//@Override
public int compare(RoleImpl r1, RoleImpl r2) {
return Integer.compare(r1.getId(), r2.getId());
}
});
for (RoleImpl r : roles) {
hashedMappings.put(r, new ArrayList<RoleMapping>());
}
for (RoleMapping rm : roleMappings) {
hashedMappings.get(rm.getRole()).add(rm);
}
model.addAttribute("hashedMappings", hashedMappings);
return "admin/auth/management";
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String assignRole(@Valid RoleMapping roleMapping, Model model) throws Exception {
roleService.createOrUpdate(roleMapping);
return "redirect:/admin/auth";
}
@RequestMapping(value = "/assign", method = RequestMethod.GET)
public String assignUser(@RequestParam int userId, @RequestParam int roleId, Model model) {
// TODO: Check if the signed-in user is really allowed to manage this user and group!
userService.addRoleById(userId, roleId);
return "redirect:/admin/auth";
}
@RequestMapping(value = "/unassign", method = RequestMethod.GET)
public String unassignRole(@RequestParam int userId, @RequestParam int roleId, Model model) {
// TODO: Check if the signed-in user is really allowed to manage this user and group!
userService.removeRoleById(userId, roleId);
return "redirect:/admin/auth";
}
}
package de.dariah.samlsp.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import de.dariah.aai.javasp.base.SimpleUserDetails;
import de.dariah.aai.javasp.web.service.RoleService;
import de.dariah.aai.web.controller.ExceptionHandlingController;
import de.dariah.samlsp.orm.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController extends ExceptionHandlingController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
// Conversion between name representation and Role.class
//binder.registerCustomEditor(RoleImpl.class, new RoleEditor(roleService.getStaticRoles()));
// Trim string values and set "" to null - helps if Null values are allowed
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirectUser(HttpServletRequest request, Model model) {
model.addAttribute("homeOrganisationEntityID", request.getSession().getAttribute("entityId"));
model.addAttribute("reqestedURL", request.getSession().getAttribute("originalRequestURI"));
return "user/redirect";
}
@Secured(value="IS_AUTHENTICATED_FULLY")
@RequestMapping(value = "/profile", method = RequestMethod.GET)
public String showProfile(Model model) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
SimpleUserDetails user = null;
if (auth != null && auth.getDetails() instanceof SimpleUserDetails) {
user = (SimpleUserDetails)auth.getDetails();
if (user != null) {
model.addAttribute("user", userService.findById(user.getId()));
model.addAttribute("authorityList", user.getAuthorities());
return "user/profile";
}
}
return "redirect:/saml/login";
}
}
package de.dariah.samlsp.orm.dao;
import java.util.Hashtable;
import de.dariah.aai.javasp.base.Role;
import de.dariah.base.dao.base.BaseEntityDao;
import de.dariah.samlsp.orm.model.RoleImpl;
public interface RoleDao extends BaseEntityDao<RoleImpl> {
public Hashtable<String, Role> getStaticRoles();
}
\ No newline at end of file
package de.dariah.samlsp.orm.dao;
import java.util.Hashtable;
import org.springframework.stereotype.Repository;
import de.dariah.aai.javasp.base.Role;
import de.dariah.base.dao.base.BaseEntityDaoImpl;
import de.dariah.samlsp.orm.model.RoleImpl;
@Repository
public class RoleDaoImpl extends BaseEntityDaoImpl<RoleImpl> implements RoleDao {
private Hashtable<String, Role> staticRoles = new Hashtable<String, Role>();
public Hashtable<String, Role> getStaticRoles() { return staticRoles; }
public RoleDaoImpl() {
super(RoleImpl.class);
}
}
package de.dariah.samlsp.orm.dao;
import de.dariah.base.dao.base.BaseEntityDao;
import de.dariah.samlsp.orm.model.RoleMapping;
public interface RoleMappingDao extends BaseEntityDao<RoleMapping> {
public RoleMapping findByName(String endpoint, String name) throws Exception;
}
package de.dariah.samlsp.orm.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import de.dariah.base.dao.base.BaseEntityDaoImpl;
import de.dariah.samlsp.orm.model.RoleMapping;
@Repository
public class RoleMappingDaoImpl extends BaseEntityDaoImpl<RoleMapping> implements RoleMappingDao {
public RoleMappingDaoImpl() {
super(RoleMapping.class);
}
//@Override
public RoleMapping findByName(String endpoint, String name) throws Exception {
ArrayList<Criterion> criterionList = new ArrayList<Criterion>();
criterionList.add(Restrictions.eq("endpoint", endpoint));
criterionList.add(Restrictions.eq("name", name));
List<RoleMapping> roleMappings = findByCriteria(criterionList);
if (roleMappings==null || roleMappings.size()==0) {
return null;
} else if (roleMappings.size()>1) {
throw new Exception("Inconsistent database state; 'endpoint' and 'name' need to be unique in table for roleMappings");
}
return roleMappings.get(0);
}
}
package de.dariah.samlsp.orm.dao;
import java.util.List;
import de.dariah.base.dao.base.BaseEntityDao;
import de.dariah.samlsp.orm.model.UserImpl;
public interface UserDao extends BaseEntityDao<UserImpl> {
public UserImpl findByName(String endpoint, String name) throws Exception;
public List<UserImpl> findByProperty(String property, String value, boolean caseInsensitive);
public List<String> findEppnsByQuery(String query);
}
package de.dariah.samlsp.orm.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import de.dariah.base.dao.base.BaseEntityDaoImpl;
import de.dariah.samlsp.orm.model.UserImpl;
@Repository
public class UserDaoImpl extends BaseEntityDaoImpl<UserImpl> implements UserDao {
public UserDaoImpl() {
super(UserImpl.class);
}
@Override
public UserImpl findByName(String endpoint, String nameId) throws Exception {
ArrayList<Criterion> cr = new ArrayList<Criterion>();
cr.add(Restrictions.eq("endpointId", endpoint));
cr.add(Restrictions.eq("nameId", nameId));
List<UserImpl> resultList = findByCriteria(cr);
if (resultList == null || resultList.size() == 0) {
return null;
}
else if (resultList.size() > 1) {
throw new Exception("Could not execute distinct query; expected 1 row, received: " + resultList.size());
}
return resultList.get(0);
}
@Override
public List<UserImpl> findByProperty(String property, String value, boolean caseInsensitive) {
ArrayList<Criterion> cr = new ArrayList<Criterion>();
cr.add(Restrictions.eq(property, value).ignoreCase());
return findByCriteria(cr);
}
@Override
public <T> List<T> findPropertyValuesDistinct(String property, Class<T> returnType) {
Criteria criteria = getCurrentSession().createCriteria(UserImpl.class);
criteria.setProjection(Projections.distinct(Projections.property(property)));
List<?> l = criteria.list();
if (l==null) {
return null;
}
List<T> result = new ArrayList<T>(l.size());
for (Object o : l) {
result.add(returnType.cast(o));
}
return result;
}
@Override
public List<String> findEppnsByQuery(String query) {
Criteria criteria = getCurrentSession().createCriteria(UserImpl.class);
criteria.setProjection(Projections.distinct(Projections.property("eduPersonPrincipalName")));
criteria.add(Restrictions.ilike("eduPersonPrincipalName", "%" + query + "%"));
List<?> l = criteria.list();
if (l==null) {
return null;
}
List<String> eppns = new ArrayList<String>(l.size());
for (Object o : l) {
eppns.add(String.class.cast(o));
}
return eppns;
}
}
package de.dariah.samlsp.orm.model;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;