Skip to content

Commit 2521dad

Browse files
authored
Use constants for system settings names (#329)
Fixes #324
1 parent f687344 commit 2521dad

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

api/v1/controllers/login.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export async function login (req, res, next) {
119119
if (!await Auth.isAdmin(user.id)) {
120120
const settings = await Settings.get(Const.PW_USER_ADMINID)
121121
for (const setting of settings) {
122-
if (setting.setting === 'systemlock' && setting.value === '1') {
122+
if (setting.setting === Const.SYSTEM_LOCK && setting.value === '1') {
123123
res.status(R.UNAUTHORIZED).send(R.ko('System is locked, retry later'))
124124
return
125125
}

api/v1/controllers/util.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export async function systemLock (req, res, next) {
102102
return
103103
}
104104

105-
await Settings.set(Const.PW_USER_ADMINID, 'systemlock', '1')
105+
await Settings.set(Const.PW_USER_ADMINID, Const.SYSTEM_LOCK, '1')
106106
Config.generateJWTKey()
107107

108108
res.send(R.ok())
@@ -121,7 +121,7 @@ export async function systemUnlock (req, res, next) {
121121
return
122122
}
123123

124-
await Settings.set(Const.PW_USER_ADMINID, 'systemlock', '0')
124+
await Settings.set(Const.PW_USER_ADMINID, Const.SYSTEM_LOCK, '0')
125125

126126
res.send(R.ok())
127127
}
@@ -139,7 +139,7 @@ export async function systemReadOnly (req, res, next) {
139139
return
140140
}
141141

142-
await Cache.set(Const.PW_USER_ADMINID, 'readonly', true)
142+
await Cache.set(Const.PW_USER_ADMINID, Const.SYSTEM_READONLY, true)
143143

144144
res.send(R.ok())
145145
}
@@ -157,7 +157,7 @@ export async function systemReadWrite (req, res, next) {
157157
return
158158
}
159159

160-
await Cache.set(Const.PW_USER_ADMINID, 'readonly', false)
160+
await Cache.set(Const.PW_USER_ADMINID, Const.SYSTEM_READONLY, false)
161161

162162
res.send(R.ok())
163163
}
@@ -169,7 +169,7 @@ export async function systemReadWrite (req, res, next) {
169169
* @param {Object} next Express next
170170
*/
171171
export async function systemGetReadOnly (req, res, next) {
172-
const readonly = await Cache.get(Const.PW_USER_ADMINID, 'readonly')
172+
const readonly = await Cache.get(Const.PW_USER_ADMINID, Const.SYSTEM_READONLY)
173173
res.send(R.ok({ readonly }))
174174
}
175175

@@ -180,9 +180,9 @@ export async function systemGetReadOnly (req, res, next) {
180180
* @param {Object} next Express next
181181
*/
182182
export async function systemGetLock (req, res, next) {
183-
const settings = await Settings.get(Const.PW_USER_ADMINID, 'systemlock')
183+
const settings = await Settings.get(Const.PW_USER_ADMINID, Const.SYSTEM_LOCK)
184184
for (const setting of settings) {
185-
if (setting.setting === 'systemlock') {
185+
if (setting.setting === Const.SYSTEM_LOCK) {
186186
res.send(R.ok({ locked: setting.value === '1' }))
187187
return
188188
}

lib/auth.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,5 @@ export function validatePersonalToken (personaltoken) {
145145
* Return read only mode from cache
146146
*/
147147
export function isReadOnly () {
148-
return Cache.get(Const.PW_USER_ADMINID, 'readonly')
148+
return Cache.get(Const.PW_USER_ADMINID, Const.SYSTEM_READONLY)
149149
}

lib/const.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ export const KMS_TYPE_NODEK = 0
107107
export const KMS_TYPE_LOCALFILE = 1
108108
export const KMS_TYPE_GOOGLECLOUD = 2
109109

110+
export const SYSTEM_LOCK = 'systemlock'
111+
export const SYSTEM_READONLY = 'readonly'
112+
110113
export const METRICS_LOGIN_USERS = 'login_users_total'
111114
export const METRICS_LOGIN_APIKEYS = 'login_apikeys_total'
112115
export const METRICS_ITEMS_READ = 'items_read_total'

passweaver-api.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const cfg = Config.get()
4949
await Cache.init()
5050

5151
// Set readonly flag in cache
52-
Cache.set(Const.PW_USER_ADMINID, 'readonly', Config.get().readonly)
52+
Cache.set(Const.PW_USER_ADMINID, Const.SYSTEM_READONLY, Config.get().readonly)
5353

5454
// Rate limiter
5555
app.use(rateLimitMiddleware)

0 commit comments

Comments
 (0)