Commit b94ce712 authored by Sandro Lutz's avatar Sandro Lutz
Browse files

Update password change form (Resolves #5)

parent c37303cc
......@@ -25,6 +25,7 @@ export default class InputGroup {
args = {};
}
args.value = vnode.attrs.value;
args.type = vnode.attrs.type;
args.onchange = vnode.attrs.onchange;
args.oninput = vnode.attrs.oninput;
args.getSuggestions = vnode.attrs.getSuggestions;
......
import m from 'mithril';
import { isLoggedIn } from '../models/auth';
import { isLoggedIn, getUsername, login } from '../models/auth';
import * as user from '../models/user';
import * as groups from '../models/groups';
import inputGroup from './form/inputGroup';
import { Button } from '../components';
// shows all relevant user information
......@@ -26,16 +27,30 @@ class showUserInfo {
// provides a form to change the users password (if not authenticated by LDAP)
class changePasswordForm {
oninit() {
this.password_old = '';
this.password1 = '';
this.password2 = '';
}
submit() {
const password = this.password1;
this.busy = true;
// renew authentication token, so we are allowed to change the password
login(getUsername(), this.password_old);
user
.update({ password })
.then(() => {
this.busy = false;
this.password_old = '';
this.password1 = '';
this.password2 = '';
})
.catch(() => {
this.busy = false;
this.password_old = '';
this.password2 = '';
});
}
......@@ -47,7 +62,8 @@ class changePasswordForm {
// * Does not contain any whitespace characters
validate() {
this.valid =
this.password1.length > 8 &&
this.password_old.length > 0 &&
this.password1.length >= 8 &&
this.password1.length <= 100 &&
!this.password1.match(/\s/g) &&
this.password1.match(/[A-Z]/g) &&
......@@ -60,36 +76,73 @@ class changePasswordForm {
view() {
if (user.get() === undefined) return m();
const buttonArgs = { events: { onclick: () => this.submit() } };
const buttonArgs = {};
let buttons;
if (!this.valid || !this.equal || this.busy) {
buttonArgs.disabled = true;
}
if (user.get().password_set) {
buttons = [
m(Button, {
...buttonArgs,
label: 'change password',
events: { onclick: () => this.submit() },
}),
m(Button, {
disabled: this.password_old.length === 0,
label: 'Revert to LDAP',
events: {
onclick: () => {
this.password1 = '';
this.password2 = '';
this.submit();
},
},
}),
];
} else {
buttons = m(Button, {
...buttonArgs,
label: 'set password',
events: { onclick: () => this.submit() },
});
}
return m('div', [
m('input', {
m('div', 'Requirements: min 8 characters, upper and lower case letters and numbers'),
m(inputGroup, {
name: 'password_old',
title: 'Old Password',
type: 'password',
value: this.password_old,
oninput: e => {
this.password_old = e.target.value;
this.validate();
},
}),
m(inputGroup, {
name: 'password1',
id: 'password1',
title: 'New Password',
type: 'password',
placeholder: 'Password',
value: this.password1,
oninput: e => {
this.password1 = e.target.value;
this.validate();
},
}),
m('input', {
m(inputGroup, {
name: 'password2',
id: 'password2',
title: 'Repeat',
type: 'password',
placeholder: 'Repeat',
value: this.password2,
oninput: e => {
this.password2 = e.target.value;
this.validate();
},
}),
m(Button, { ...buttonArgs, label: 'change password' }),
buttons,
]);
}
}
......@@ -120,11 +173,9 @@ class rfidForm {
}
return m('div', [
m('input', {
m(inputGroup, {
name: 'rfid',
id: 'rfid',
type: 'text',
placeholder: 'RFID',
title: 'RFID',
value: this.rfid,
oninput: e => {
this.rfid = e.target.value;
......
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