Commit 9bf91dc6 by Tuomas Riihimäki

Merge branch 'master' of dev.intra.insomnia.fi:/data/bortal

2 parents eab4f333 80e5e6c2
......@@ -46,6 +46,9 @@ public class RoleBean implements RoleBeanLocal {
public List<Role> getPossibleParents(Role role) {
List<Role> roleList = listRoles();
if(role == null)
return roleList;
List<Role> children = getAllChilds(role, new HashSet<Role>());
for (Role unit : children) {
......@@ -63,7 +66,7 @@ public class RoleBean implements RoleBeanLocal {
List<Role> returnList = new ArrayList<Role>();
if (checkedRoles.contains(role)) {
if (checkedRoles.contains(role) || role == null) {
return returnList;
}
......
package fi.insomnia.bortal.beans;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
......@@ -13,6 +14,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.facade.AccessRightFacade;
import fi.insomnia.bortal.facade.RoleFacade;
import fi.insomnia.bortal.facade.UserFacade;
import fi.insomnia.bortal.model.AccessRight;
import fi.insomnia.bortal.model.Role;
......@@ -30,6 +32,8 @@ public class SessionHandlerBean implements SessionHandlerBeanLocal {
private UserFacade userfacade;
@EJB
private AccessRightFacade accessRightFacade;
@EJB
private RoleFacade roleFacade;
/**
* Default constructor.
......@@ -38,7 +42,6 @@ public class SessionHandlerBean implements SessionHandlerBeanLocal {
// TODO Auto-generated constructor stub
}
@Override
public boolean hasPermission(String target, User user, RolePermission permission) {
......@@ -97,14 +100,25 @@ public class SessionHandlerBean implements SessionHandlerBeanLocal {
public User tryLogin(String username, String password) {
User user = userfacade.findByLogin(username.trim());
if(user != null && user.checkPassword(password)) {
if (user != null && user.checkPassword(password)) {
return user;
}
return null;
}
/**
* Makes sure default user and public role exist and the user is member of
* the role.
*/
public User getDefaultUser() {
throw new UnsupportedOperationException("Not supported yet.");
Role publicRole = roleFacade.getOrCreatePublicRole();
User defaultUser = userfacade.getOrCreateDefaultUser();
if (!defaultUser.getRoles().contains(publicRole)) {
ArrayList<Role> userRoles = new ArrayList<Role>();
userRoles.add(publicRole);
defaultUser.setRoles(userRoles);
}
return defaultUser;
}
}
......@@ -60,5 +60,4 @@ public class UserBean implements UserBeanLocal {
public User getUser(String nick) {
return userFacade.findByLogin(nick);
}
}
......@@ -4,6 +4,8 @@ import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import fi.insomnia.bortal.model.Product;
@Stateless
......@@ -21,4 +23,8 @@ public class ProductFacade extends GenericFacade<Product> {
return em;
}
public Product getCreditProduct() {
throw new NotImplementedException();
}
}
......@@ -4,12 +4,16 @@ import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import fi.insomnia.bortal.model.Role;
import fi.insomnia.bortal.model.User;
@Stateless
@LocalBean
public class RoleFacade extends GenericFacade<Role> {
private static final String PUBLIC_ROLE_NAME = "public";
@PersistenceContext
private EntityManager em;
......@@ -21,4 +25,26 @@ public class RoleFacade extends GenericFacade<Role> {
return em;
}
public Role findByName(String name) {
TypedQuery<Role> q = em.createNamedQuery("User.findByName", Role.class);
q.setParameter("name", name);
return q.getSingleResult();
}
public Role getOrCreatePublicRole() {
Role publicRole = findByName(PUBLIC_ROLE_NAME);
if (publicRole == null) {
publicRole = createPublicRole();
}
return publicRole;
}
private Role createPublicRole() {
Role publicRole = new Role();
publicRole.setName(PUBLIC_ROLE_NAME);
em.persist(publicRole);
return publicRole;
}
}
......@@ -31,7 +31,7 @@ public class UserFacade extends GenericFacade<User> {
return q.getSingleResult();
}
public User getDefaultUser() {
public User getOrCreateDefaultUser() {
User defaultUser = findByLogin(DEFAULT_USER_LOGIN);
// Create if doesn't exist yet
......
......@@ -12,5 +12,6 @@
<attribute name="owner.project.facets" value="jst.java"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/LanBortalUtilities"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
......@@ -4,6 +4,7 @@
*/
package fi.insomnia.bortal.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
......@@ -19,58 +20,52 @@ import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
/**
*
*/
@Entity
@Table(name = "roles")
@Table(name = "roles", uniqueConstraints = {
@UniqueConstraint(columnNames = {"events_pk_id", "role_name"})})
@NamedQueries({
@NamedQuery(name = "Role.findAll", query = "SELECT r FROM Role r"),
@NamedQuery(name = "Role.findByRoleName", query = "SELECT r FROM Role r WHERE r.name = :name") })
@NamedQuery(name = "Role.findAll", query = "SELECT r FROM Role r"),
@NamedQuery(name = "Role.findByRoleName", query = "SELECT r FROM Role r WHERE r.name = :name")})
public class Role implements EventChildInterface {
private static final long serialVersionUID = 1L;
@EmbeddedId
private EventPk id;
@Column(name = "role_name", nullable = false)
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "role_memberships", joinColumns = {@JoinColumn(name = "roles_id", referencedColumnName = "entity_id"),@JoinColumn(name = "roles_event_id", referencedColumnName = "events_pk_id")}, inverseJoinColumns = @JoinColumn(name = "users_id", referencedColumnName = "users_id"))
@JoinTable(name = "role_memberships", joinColumns = {
@JoinColumn(name = "roles_id", referencedColumnName = "entity_id"),
@JoinColumn(name = "roles_event_id", referencedColumnName = "events_pk_id")}, inverseJoinColumns =
@JoinColumn(name = "users_id", referencedColumnName = "users_id"))
private List<User> users;
@ManyToMany
@JoinColumns({
@JoinColumn(name = "role_id", referencedColumnName = "entity_id", nullable = false, updatable = false, insertable = false),
@JoinColumn(name = "role_event_id", referencedColumnName = "events_pk_id", nullable = false, updatable = false, insertable = false) })
private List<Role> children;
@JoinTable(name = "role_chlidren", joinColumns = {
@JoinColumn(name = "role_id", referencedColumnName = "entity_id", nullable = false, updatable = false, insertable = false),
@JoinColumn(name = "role_event_id", referencedColumnName = "events_pk_id", nullable = false, updatable = false, insertable = false)})
private List<Role> children = new ArrayList<Role>();
@ManyToMany(mappedBy = "children")
private List<Role> parents;
private List<Role> parents = new ArrayList<Role>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "role")
private List<RoleRight> roleRights;
@JoinColumns({
@JoinColumn(name = "card_template_id", referencedColumnName = "entity_id", nullable = false, updatable = false, insertable = false),
@JoinColumn(name = "card_template_event_id", referencedColumnName = "events_pk_id", nullable = false, updatable = false, insertable = false) })
@JoinColumn(name = "card_template_id", referencedColumnName = "entity_id", nullable = false, updatable = false, insertable = false),
@JoinColumn(name = "card_template_event_id", referencedColumnName = "events_pk_id", nullable = false, updatable = false, insertable = false)})
@ManyToOne
private CardTemplate cardTemplate;
@JoinColumn(name = "events_id", referencedColumnName = "events_id", nullable = false)
@ManyToOne(optional = false)
private Event event;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "role")
private List<Discount> discounts;
@ManyToMany(mappedBy = "roles")
private List<NewsGroup> newsGroups;
@Version
@Column(nullable = false)
private int jpaVersionField;
......
......@@ -26,6 +26,8 @@ import javax.persistence.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.utilities.PasswordFunctions;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
/**
......@@ -61,10 +63,10 @@ public class User implements ModelInterface<Integer> {
@Column(name = "created", nullable = false, columnDefinition = "timestamptz default now()")
@Temporal(TemporalType.TIMESTAMP)
private Calendar created;
private Calendar created = Calendar.getInstance();
@Column(name = "active", nullable = false, columnDefinition = "boolean default true")
private boolean active;
private boolean active = true;
@Column(name = "password")
private String password;
......@@ -534,11 +536,12 @@ public class User implements ModelInterface<Integer> {
}
public void resetPassword(String password) {
// XXX: Couldn't reference utility project
throw new NotImplementedException();
String newEncryptedPassword = PasswordFunctions.getEncryptedPassword(password);
this.password = newEncryptedPassword; // Bypass setter
}
public boolean checkPassword(String password) {
throw new NotImplementedException();
public boolean checkPassword(String plainPassword) {
boolean matches = PasswordFunctions.checkPlainPassword(plainPassword, this.password);
return matches;
}
}
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:role="http://java.sun.com/jsf/composite/tools/role"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition template="/layout/default-template.xhtml">
<ui:define name="title">CreateRole</ui:define>
<ui:define name="header">Add new role</ui:define>
<ui:define name="content">
<role:create />
</ui:define>
<ui:define name="footer">footer</ui:define>
</ui:composition>
</html>
\ No newline at end of file
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:role="http://java.sun.com/jsf/composite/tools/role"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition template="/layout/default-template.xhtml">
<ui:define name="title">EditRole</ui:define>
<ui:define name="header">Edit role</ui:define>
<ui:define name="content">
<role:edit />
</ui:define>
<ui:define name="footer">footer</ui:define>
</ui:composition>
</html>
\ No newline at end of file
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:role="http://java.sun.com/jsf/composite/tools/role"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition template="/layout/default-template.xhtml">
<ui:define name="title">listRoles</ui:define>
<ui:define name="header">List "roles</ui:define>
<ui:define name="content">
<role:list />
</ui:define>
<ui:define name="footer">footer</ui:define>
</ui:composition>
</html>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!