settings and users dialog
This commit is contained in:
@ -9,7 +9,17 @@ import {Observable} from 'rxjs';
|
||||
})
|
||||
export class LoginService implements CanActivate {
|
||||
|
||||
private maintainPaths = ['templates'];
|
||||
private pathPermissions = [
|
||||
{path: 'templates', permission: 'maintain'},
|
||||
{path: 'users', permission: 'admin'}
|
||||
];
|
||||
readonly levels = [
|
||||
'read',
|
||||
'write',
|
||||
'maintain',
|
||||
'dev',
|
||||
'admin'
|
||||
];
|
||||
|
||||
private loggedIn;
|
||||
private level;
|
||||
@ -23,9 +33,28 @@ export class LoginService implements CanActivate {
|
||||
|
||||
login(username = '', password = '') {
|
||||
return new Promise(resolve => {
|
||||
if (username !== '') {
|
||||
this.storage.set('basicAuth', btoa(username + ':' + password));
|
||||
console.log(username);
|
||||
console.log(password);
|
||||
if (username !== '' || password !== '') { // some credentials given
|
||||
let credentials: string[];
|
||||
const credentialString: string = this.storage.get('basicAuth');
|
||||
if (credentialString) { // found stored credentials
|
||||
credentials = atob(credentialString).split(':');
|
||||
}
|
||||
else {
|
||||
credentials = ['', ''];
|
||||
}
|
||||
if (username !== '' && password !== '') { // all credentials given
|
||||
this.storage.set('basicAuth', btoa(username + ':' + password));
|
||||
}
|
||||
else if (username !== '') { // username given
|
||||
this.storage.set('basicAuth', btoa(username + ':' + credentials[1]));
|
||||
}
|
||||
else if (password !== '') { // password given
|
||||
this.storage.set('basicAuth', btoa(credentials[0] + ':' + password));
|
||||
}
|
||||
}
|
||||
console.log(this.storage.get('basicAuth'));
|
||||
this.api.get('/authorized', (data: any, error) => {
|
||||
if (!error) {
|
||||
if (data.status === 'Authorization successful') {
|
||||
@ -53,8 +82,8 @@ export class LoginService implements CanActivate {
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot = null, state: RouterStateSnapshot = null): Observable<boolean> {
|
||||
return new Observable<boolean>(observer => {
|
||||
const isMaintainPath = this.maintainPaths.indexOf(route.url[0].path) >= 0;
|
||||
if (!isMaintainPath || (isMaintainPath && this.isMaintain)) {
|
||||
const pathPermission = this.pathPermissions.find(e => e.path.indexOf(route.url[0].path) >= 0);
|
||||
if (!pathPermission || this.is(pathPermission.permission)) { // check if level is permitted for path
|
||||
if (this.loggedIn === undefined) {
|
||||
this.login().then(res => {
|
||||
observer.next(res as any);
|
||||
@ -77,8 +106,8 @@ export class LoginService implements CanActivate {
|
||||
return this.loggedIn;
|
||||
}
|
||||
|
||||
get isMaintain() {
|
||||
return this.level === 'maintain' || this.level === 'admin';
|
||||
is(level) {
|
||||
return this.levels.indexOf(this.level) >= this.levels.indexOf(level);
|
||||
}
|
||||
|
||||
get username() {
|
||||
|
@ -66,10 +66,16 @@ export class ValidationService {
|
||||
return {ok: true, error: ''};
|
||||
}
|
||||
|
||||
string(data) {
|
||||
const {ignore, error} = Joi.string().max(128).allow('').validate(data);
|
||||
string(data, option = null) {
|
||||
let validator = Joi.string().max(128).allow('');
|
||||
let errorMsg = 'must contain max 128 characters';
|
||||
if (option === 'alphanum') {
|
||||
validator = validator.alphanum();
|
||||
errorMsg = 'must contain max 128 alphanumerical characters';
|
||||
}
|
||||
const {ignore, error} = validator.validate(data);
|
||||
if (error) {
|
||||
return {ok: false, error: 'must contain max 128 characters'};
|
||||
return {ok: false, error: errorMsg};
|
||||
}
|
||||
return {ok: true, error: ''};
|
||||
}
|
||||
@ -122,6 +128,13 @@ export class ValidationService {
|
||||
return {ok: true, error: ''};
|
||||
}
|
||||
|
||||
equal(data, compare) {
|
||||
if (data !== compare) {
|
||||
return {ok: false, error: `must be equal`};
|
||||
}
|
||||
return {ok: true, error: ''};
|
||||
}
|
||||
|
||||
parameterName(data) {
|
||||
const {ignore, error} = Joi.string()
|
||||
.max(128)
|
||||
|
Reference in New Issue
Block a user