forked from pik-gane/vodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.service.ts
178 lines (142 loc) · 5.75 KB
/
settings.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
(C) Copyright 2015–2022 Potsdam Institute for Climate Impact Research (PIK), authors, and contributors, see AUTHORS file.
This file is part of vodle.
vodle is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
vodle is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License
along with vodle. If not, see <https://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { ValidationErrors, AbstractControl } from '@angular/forms';
import { environment } from '../environments/environment';
import { GlobalService } from './global.service';
@Injectable({
providedIn: 'root'
})
export class SettingsService {
notification_classes = [
'delegation_accepted',
'delegation_declined',
'new_option',
'poll_closing_soon',
'poll_closed'
];
private G: GlobalService;
use_guest = false;
constructor() { }
init(G:GlobalService) {
this.G = G;
}
// properties:
public get consent(): boolean { return this.G.D.getu('consent') != "0"; }
public set consent(value: boolean) { this.G.D.setu('consent', value ? "1": "0"); }
public get email(): string { return this.G.D.getu('email'); }
public set email(value: string) { this.G.D.setu('email', value); }
public get password(): string { return this.G.D.getu('password'); }
public set password(value: string) { this.G.D.setu('password', value); }
public get db(): string { return this.G.D.getu('db'); }
public set db(value: string) {
this.G.D.setu('db', value);
this.compute_db_credentials();
}
public get db_from_pid(): string { return this.G.D.getu('db_from_pid'); }
public set db_from_pid(value: string) {
this.G.D.setu('db_from_pid', value);
this.compute_db_credentials();
}
public get db_custom_server_url(): string { return this.G.D.getu('db_custom_server_url'); }
public set db_custom_server_url(value: string) {
this.G.D.setu('db_custom_server_url', value);
this.compute_db_credentials();
}
public get db_custom_password(): string { return this.G.D.getu('db_custom_password'); }
public set db_custom_password(value: string) {
this.G.D.setu('db_custom_password', value);
this.compute_db_credentials();
}
public get db_server_url(): string { return this.G.D.getu('db_server_url'); }
private set db_server_url(value: string) {
// will be set automatically
this.G.D.setu('db_server_url', value);
}
public get db_password(): string { return this.G.D.getu('db_password'); }
private set db_password(value: string) {
// will be set automatically
this.G.D.setu('db_password', value);
}
public get language(): string { return this.G.D.getu('language'); }
public set language(value: string) { this.G.D.setu('language', value); }
public get theme(): string { return this.G.D.getu('theme'); }
public set theme(value: string) { this.G.D.setu('theme', value); }
public get default_wap(): number { return Number.parseInt(this.G.D.getu('default_wap')||'0'); }
public set default_wap(value: number) { this.G.D.setu('default_wap', value.toString()); }
get_notify_of(cls: string): boolean { return this.G.D.getu('notify_of_'+cls) != "0"; } // by default, all notifications are on
set_notify_of(cls: string, value: boolean) { this.G.D.setu('notify_of_'+cls, value ? "1": "0"); }
closing_soon_fraction = 1/7; // TODO: turn into settings option
// other data:
// public password_regexp = '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$';
public password_regexp = '(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*';
public passwords_match(control: AbstractControl): ValidationErrors | null {
// password validation function to be used in forms
if (control) {
const password = control.get('password');
const confirm_password = control.get('confirm_password');
if (password.errors) {
return (password.errors);
}
if (confirm_password.value !== password.value) {
return ({must_match: true});
}
}
return null;
}
// OTHER METHODS:
public compute_db_credentials() {
// set db credentials according to this.db... settings:
var url;
if (this.db=='central') {
url = environment.data_service.central_db_server_url;
this.db_password = environment.data_service.central_db_password;
} else if (this.db=='poll') {
url = this.G.P.polls[this.db_from_pid].db_server_url;
this.db_password = this.G.P.polls[this.db_from_pid].db_password;
} else if (this.db=='other') {
url = this.db_custom_server_url;
this.db_password = this.db_custom_password;
}
this.db_server_url = this.G.D.fix_url(url);
}
// OTHER CONSTANTS:
language_names = { // TODO: keep in "phonetically alphabetical" order
de: 'Deutsch',
en: 'English',
es: 'Español',
// fr: 'Français',
// hi: 'हिन्दी',
ko: '한국어',
pl: 'Polski',
// fi: 'Suomi',
// zh: '中文'
nn: '[JSON file keys]'
};
validation_messages = {
email: [
{ type: 'required', message: 'validation.email-required' },
{ type: 'email', message: 'validation.email-valid' }
],
password: [
{ type: 'required', message: 'validation.password-required' },
{ type: 'minlength', message: 'validation.password-length' },
{ type: 'pattern', message: 'validation.password-pattern' }
],
passwords_match: [
{ message: 'validation.passwords-match' }
],
};
}