multi user

This commit is contained in:
2019-09-24 18:18:27 +02:00
parent bab71445a8
commit dbd3e42466
7 changed files with 175 additions and 21 deletions

View File

@ -22,7 +22,7 @@ apply plugin: 'eclipse'
apply plugin: 'nu.studer.jooq' apply plugin: 'nu.studer.jooq'
group = 'jottyfan' group = 'jottyfan'
version = '1.0.7' version = '1.0.8'
description = """timetrack""" description = """timetrack"""

View File

@ -2,6 +2,8 @@ package de.jottyfan.timetrack.modules;
import java.sql.SQLException; import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import org.jooq.DSLContext; import org.jooq.DSLContext;
import org.jooq.TableLike; import org.jooq.TableLike;
@ -23,10 +25,19 @@ public class JooqGateway {
return (DSLContext) facesContext.getJooq(); return (DSLContext) facesContext.getJooq();
} }
public void addToSessionMap(String key, Object value) {
facesContext.getExternalContext().getSessionMap().put(key, value);
}
public Integer getFkLogin() { public Integer getFkLogin() {
// TODO: make a login, add the profile id to the session and read it here from SessionBean bean = (SessionBean) facesContext.getExternalContext().getSessionMap().get("sessionBean");
// facesContext if (bean == null) {
return 1; facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "schwerer Anwendungsfehler",
"no sessionBean found in session, therefore, set fkLogin to 1"));
bean = new SessionBean();
bean.setLogin(1);
}
return bean.getLogin();
} }
/** /**

View File

@ -1,6 +1,8 @@
package de.jottyfan.timetrack.modules; package de.jottyfan.timetrack.modules;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.enterprise.context.SessionScoped; import javax.enterprise.context.SessionScoped;
import javax.inject.Named; import javax.inject.Named;
@ -17,12 +19,17 @@ import org.jasypt.util.password.StrongPasswordEncryptor;
public class SessionBean implements Serializable { public class SessionBean implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Set<String> privileges;
private Integer login; private Integer login;
private String username; private String username;
private String secret; private String secret;
private String forename; private String forename;
private String surname; private String surname;
public SessionBean() {
this.privileges = new HashSet<>();
}
public Boolean getHasLogin() { public Boolean getHasLogin() {
return login != null; return login != null;
} }
@ -39,6 +46,10 @@ public class SessionBean implements Serializable {
this.login = login; this.login = login;
} }
public Boolean hasPrivilege(String privilege) {
return privileges.contains(privilege);
}
public String getSecret() { public String getSecret() {
StrongPasswordEncryptor spe = new StrongPasswordEncryptor(); StrongPasswordEncryptor spe = new StrongPasswordEncryptor();
return spe.encryptPassword(secret); return spe.encryptPassword(secret);
@ -76,4 +87,11 @@ public class SessionBean implements Serializable {
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.username = username;
} }
/**
* @return the privileges
*/
public Set<String> getPrivileges() {
return privileges;
}
} }

View File

@ -1,6 +1,7 @@
package de.jottyfan.timetrack.modules; package de.jottyfan.timetrack.modules;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -20,6 +21,9 @@ public class SessionControl {
@Inject @Inject
private SessionBean sessionBean; private SessionBean sessionBean;
private String secretNew1;
private String secretNew2;
public String doLogin() { public String doLogin() {
SessionModel model = new SessionModel(); SessionModel model = new SessionModel();
model.doLogin((JooqFacesContext) FacesContext.getCurrentInstance(), sessionBean); model.doLogin((JooqFacesContext) FacesContext.getCurrentInstance(), sessionBean);
@ -32,6 +36,56 @@ public class SessionControl {
sessionBean.setForename(null); sessionBean.setForename(null);
sessionBean.setSurname(null); sessionBean.setSurname(null);
sessionBean.setUsername(null); sessionBean.setUsername(null);
sessionBean.getPrivileges().clear();
return Pages.START.get(); return Pages.START.get();
} }
public String doChangePassword() {
if (!secretNew1.equals(secretNew2)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "error on changing password",
"passwords are not equal");
FacesContext.getCurrentInstance().addMessage(null, msg);
return "";
} else if (new SessionModel().doChangePassword((JooqFacesContext) FacesContext.getCurrentInstance(), secretNew1)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "successfully changed password",
"the new password has been saved");
FacesContext.getCurrentInstance().addMessage(null, msg);
return Pages.START.get();
} else {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "error on changing password",
"database change was not successful");
FacesContext.getCurrentInstance().addMessage(null, msg);
return "";
}
}
/**
* @return the secretNew1
*/
public String getSecretNew1() {
return secretNew1;
}
/**
* @param secretNew1
* the secretNew1 to set
*/
public void setSecretNew1(String secretNew1) {
this.secretNew1 = secretNew1;
}
/**
* @return the secretNew2
*/
public String getSecretNew2() {
return secretNew2;
}
/**
* @param secretNew2
* the secretNew2 to set
*/
public void setSecretNew2(String secretNew2) {
this.secretNew2 = secretNew2;
}
} }

View File

@ -1,17 +1,21 @@
package de.jottyfan.timetrack.modules; package de.jottyfan.timetrack.modules;
import static de.jottyfan.timetrack.db.profile.Tables.T_LOGIN; import static de.jottyfan.timetrack.db.profile.Tables.T_LOGIN;
import static de.jottyfan.timetrack.db.profile.Tables.V_LOGINROLE;
import java.sql.SQLException; import java.sql.SQLException;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.jooq.DSLContext; import org.jooq.DSLContext;
import org.jooq.Record1;
import org.jooq.Record4; import org.jooq.Record4;
import org.jooq.SelectConditionStep; import org.jooq.SelectConditionStep;
import org.jooq.UpdateConditionStep;
import org.jooq.exception.DataAccessException; import org.jooq.exception.DataAccessException;
import de.jooqFaces.JooqFacesContext; import de.jooqFaces.JooqFacesContext;
import de.jottyfan.timetrack.db.profile.tables.records.TLoginRecord;
/** /**
* *
@ -54,6 +58,18 @@ public class SessionGateway extends JooqGateway {
bean.setLogin(r.get(T_LOGIN.PK)); bean.setLogin(r.get(T_LOGIN.PK));
bean.setForename(r.get(T_LOGIN.FORENAME)); bean.setForename(r.get(T_LOGIN.FORENAME));
bean.setSurname(r.get(T_LOGIN.SURNAME)); bean.setSurname(r.get(T_LOGIN.SURNAME));
SelectConditionStep<Record1<String>> sql2 = jooq
// @formatter:off
.select(V_LOGINROLE.ROLE_NAME)
.from(V_LOGINROLE)
.where(V_LOGINROLE.LOGIN.eq(bean.getUsername()));
// @formatter:on
LOGGER.debug(sql2.toString());
for (Record1<String> privilege : sql2.fetch()) {
bean.getPrivileges().add(privilege.get(V_LOGINROLE.ROLE_NAME));
}
addToSessionMap("sessionBean", bean);
return true; return true;
} else { } else {
throw new DataAccessException("wrong password"); throw new DataAccessException("wrong password");
@ -64,4 +80,29 @@ public class SessionGateway extends JooqGateway {
} }
} }
/**
* change the password
*
* @param bean
* the bean containing the new password
* @return true or false
* @throws SQLException
* @throws ClassNotFoundException
* @throws DataAccessException
*/
public boolean changePassword(SessionBean bean, String newPassword)
throws DataAccessException, ClassNotFoundException, SQLException {
bean.setSecret(newPassword);
String encryptedPassword = bean.getSecret();
try (DSLContext jooq = getJooq()) {
UpdateConditionStep<TLoginRecord> sql = jooq
// @formatter:off
.update(T_LOGIN)
.set(T_LOGIN.PASSWORD, encryptedPassword)
.where(T_LOGIN.PK.eq(bean.getLogin()));
// @formatter:on
LOGGER.debug("updating password, disable log here");
return sql.execute() == 1;
}
}
} }

View File

@ -25,4 +25,14 @@ public class SessionModel {
} }
} }
public boolean doChangePassword(JooqFacesContext facesContext, String secretNew) {
try {
SessionBean bean = (SessionBean) facesContext.getExternalContext().getSessionMap().get("sessionBean");
return new SessionGateway(facesContext).changePassword(bean, secretNew);
} catch (DataAccessException | ClassNotFoundException | SQLException e) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "error on changing password", e.getMessage());
facesContext.addMessage(null, msg);
return false;
}
}
} }

View File

@ -1,7 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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" <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:f="http://java.sun.com/jsf/core" xmlns:my="http://xmlns.jcp.org/jsf/composite/my" xmlns:b="http://bootsfaces.net/ui"> xmlns:my="http://xmlns.jcp.org/jsf/composite/my" xmlns:b="http://bootsfaces.net/ui">
<h:head> <h:head>
</h:head> </h:head>
<h:body> <h:body>
@ -11,12 +11,31 @@
</ui:define> </ui:define>
<ui:define name="main"> <ui:define name="main">
<b:panel title="Einstellungen" collapsed="true" rendered="#{sessionBean.hasLogin}"> <b:panel title="Einstellungen" collapsed="true" rendered="#{sessionBean.hasLogin}">
<b:form> <b:accordion expandedPanels="layout">
<b:selectOneMenu value="#{themeBean.currentTheme}"> <b:panel id="layout" title="Layout">
<f:selectItems value="#{themeBean.validThemes}" var="t" itemValue="#{t}" itemLabel="#{t}" /> <b:form>
</b:selectOneMenu> <b:selectOneMenu value="#{themeBean.currentTheme}">
<b:commandButton action="#{doneControl.toStart}" value="ändern" iconAwesome="pencil" look="warning" /> <f:selectItems value="#{themeBean.validThemes}" var="t" itemValue="#{t}" itemLabel="#{t}" />
</b:form> </b:selectOneMenu>
<b:commandButton action="#{doneControl.toStart}" value="ändern" iconAwesome="pencil" look="warning" />
</b:form>
</b:panel>
<b:panel id="password" title="Passwort">
<b:form>
<b:inputSecret value="#{sessionControl.secretNew1}" required="true" requiredMessage="Das Feld Passwort muss ausgefüllt werden.">
<f:facet name="prepend">
<h:outputText value="Passwort" />
</f:facet>
</b:inputSecret>
<b:inputSecret value="#{sessionControl.secretNew2}" required="true" requiredMessage="Das Feld Passwort nochmal muss ausgefüllt werden.">
<f:facet name="prepend">
<h:outputText value="Passwort nochmal" />
</f:facet>
</b:inputSecret>
<b:commandButton action="#{sessionControl.doChangePassword}" value="übernehmen" look="warning" />
</b:form>
</b:panel>
</b:accordion>
</b:panel> </b:panel>
<b:panel title="Login" rendered="#{sessionBean.hasNoLogin}" styleClass="loginpanel"> <b:panel title="Login" rendered="#{sessionBean.hasNoLogin}" styleClass="loginpanel">
<b:form> <b:form>
@ -25,6 +44,7 @@
<h:outputText value="Username" /> <h:outputText value="Username" />
</f:facet> </f:facet>
<f:selectItem itemValue="henkej" itemLabel="Jörg Henke" /> <f:selectItem itemValue="henkej" itemLabel="Jörg Henke" />
<f:selectItem itemValue="piontekm" itemLabel="Michael Piontek" />
</b:selectOneMenu> </b:selectOneMenu>
<b:inputSecret value="#{sessionBean.secret}"> <b:inputSecret value="#{sessionBean.secret}">
<f:facet name="prepend"> <f:facet name="prepend">
@ -38,11 +58,11 @@
<ui:define name="navigation"> <ui:define name="navigation">
<b:form rendered="#{sessionBean.hasLogin}"> <b:form rendered="#{sessionBean.hasLogin}">
<b:buttonGroup> <b:buttonGroup>
<b:commandButton action="#{noteControl.toList}" value="#{noteControl.amount} Notizen verwalten" look="primary" <b:commandButton action="#{noteControl.toList}" value="#{noteControl.amount} Notizen verwalten" look="primary" iconAwesome="comments-o"
iconAwesome="comments-o" /> rendered="#{sessionBean.hasPrivilege('write_note')}" />
<b:commandButton action="#{contactControl.toList}" value="#{contactControl.amount} Kontakte verwalten" look="primary" <b:commandButton action="#{contactControl.toList}" value="#{contactControl.amount} Kontakte verwalten" look="primary" iconAwesome="group"
iconAwesome="group" /> rendered="#{sessionBean.hasPrivilege('write_contact')}" />
<b:commandButton action="#{doneControl.toList}" value="Arbeitszeit verwalten" look="primary" iconAwesome="clock-o" /> <b:commandButton action="#{doneControl.toList}" value="Arbeitszeit verwalten" look="primary" iconAwesome="clock-o" rendered="#{sessionBean.hasPrivilege('write_done')}" />
<b:commandButton action="#{sessionControl.doLogout}" value="abmelden" look="danger" iconAwesome="sign-out" /> <b:commandButton action="#{sessionControl.doLogout}" value="abmelden" look="danger" iconAwesome="sign-out" />
</b:buttonGroup> </b:buttonGroup>
</b:form> </b:form>