Commit a7f5686f authored by michael.simon's avatar michael.simon
Browse files

Add user preferences with identites with multiple accounts

parent ca22a3e6
......@@ -14,6 +14,8 @@ import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
......@@ -26,9 +28,6 @@ public class IdentityEntity extends AbstractBaseEntity {
private static final long serialVersionUID = 1L;
@Column(name="user_pref_name", length=128, unique = true)
private String userPreferredName;
@Column(name="twofa_user_id", length=512, unique = true)
private String twoFaUserId;
......@@ -38,14 +37,13 @@ public class IdentityEntity extends AbstractBaseEntity {
@OneToMany(targetEntity=UserEntity.class, mappedBy = "identity")
private Set<UserEntity> users;
public String getUserPreferredName() {
return userPreferredName;
}
public void setUserPreferredName(String userPreferredName) {
this.userPreferredName = userPreferredName;
}
@OneToMany(targetEntity=IdentityUserPreferenceEntity.class, mappedBy = "identity")
private Set<IdentityUserPreferenceEntity> userPrefs;
@ManyToOne(targetEntity = UserEntity.class)
@JoinColumn(name = "pref_user_id")
private UserEntity prefUser;
public Set<UserEntity> getUsers() {
return users;
}
......@@ -69,4 +67,20 @@ public class IdentityEntity extends AbstractBaseEntity {
public void setTwoFaUserName(String twoFaUserName) {
this.twoFaUserName = twoFaUserName;
}
public Set<IdentityUserPreferenceEntity> getUserPrefs() {
return userPrefs;
}
public void setUserPrefs(Set<IdentityUserPreferenceEntity> userPrefs) {
this.userPrefs = userPrefs;
}
public UserEntity getPrefUser() {
return prefUser;
}
public void setPrefUser(UserEntity prefUser) {
this.prefUser = prefUser;
}
}
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.entity.identity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import edu.kit.scc.webreg.entity.UserEntity;
@Entity(name = "IdentityRoleEntity")
@Table(name = "idty_role")
public class IdentityUserPreferenceEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@ManyToOne(targetEntity = IdentityEntity.class)
@JoinColumn(name = "identity_id", nullable = false)
private IdentityEntity identity;
@Id
@Column(name="pref_type", length=64)
private String prefType;
@ManyToOne(targetEntity = UserEntity.class)
@JoinColumn(name = "user_id")
private UserEntity user;
public IdentityEntity getIdentity() {
return identity;
}
public void setIdentity(IdentityEntity identity) {
this.identity = identity;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
public String getPrefType() {
return prefType;
}
public void setPrefType(String prefType) {
this.prefType = prefType;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((identity == null) ? 0 : identity.hashCode());
result = prime * result + ((prefType == null) ? 0 : prefType.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
IdentityUserPreferenceEntity other = (IdentityUserPreferenceEntity) obj;
if (identity == null) {
if (other.identity != null)
return false;
} else if (!identity.equals(other.identity))
return false;
if (prefType == null) {
if (other.prefType != null)
return false;
} else if (!prefType.equals(other.prefType))
return false;
return true;
}
}
package edu.kit.scc.webreg.service.identity;
import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.identity.IdentityEntity;
import edu.kit.scc.webreg.entity.identity.IdentityUserPreferenceEntity;
@ApplicationScoped
public class IdentityUserPrefsResolver {
public Map<String, Object> resolvePrefs(IdentityEntity identity) {
Map<String, Object> prefsMap = new HashMap<String, Object>();
if (identity.getUsers().size() == 0) {
// nothing we can do here, case should not be reachable
}
else if (identity.getUsers().size() == 1) {
// If there is only one account for identity, fill with values from account
for(UserEntity user : identity.getUsers()) {
prefsMap.put("email", user.getEmail());
prefsMap.put("surName", user.getSurName());
prefsMap.put("giveName", user.getGivenName());
prefsMap.put("eppn", user.getEppn());
}
}
else {
// Fill in from userPref Account first
UserEntity user = identity.getPrefUser();
prefsMap.put("email", user.getEmail());
prefsMap.put("surName", user.getSurName());
prefsMap.put("giveName", user.getGivenName());
prefsMap.put("eppn", user.getEppn());
// User has more than one account. Take IdentityUserPreference into account
// Overwrite the standard values from pref Account
for (IdentityUserPreferenceEntity pref : identity.getUserPrefs()) {
if (pref.getPrefType().equals("email"))
prefsMap.put(pref.getPrefType(), pref.getUser().getEmail());
else if (pref.getPrefType().equals("surName"))
prefsMap.put(pref.getPrefType(), pref.getUser().getSurName());
else if (pref.getPrefType().equals("giveName"))
prefsMap.put(pref.getPrefType(), pref.getUser().getGivenName());
else if (pref.getPrefType().equals("eppn"))
prefsMap.put(pref.getPrefType(), pref.getUser().getEppn());
}
}
return prefsMap;
}
}
......@@ -33,12 +33,6 @@
</f:facet>
<h:outputText value="#{identity.id}"/>
</p:column>
<p:column sortBy="#{identity.userPreferredName}" filterBy="#{identity.userPreferredName}">
<f:facet name="header">
<h:outputText value="#{messages.user_preferred_name}" />
</f:facet>
<h:outputText value="#{identity.userPreferredName}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{messages.eppn}" />
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment