Skip to content

Commit 0b00d5a

Browse files
committed
Make user permission buttons dynamic for new user instead of hard coded, fixed API call, increased JS number
1 parent d3aee21 commit 0b00d5a

File tree

7 files changed

+130
-59
lines changed

7 files changed

+130
-59
lines changed

build/go-generate/minifyStaticContent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,6 @@ func fileExists(filename string) bool {
137137
// Auto-generated content below, do not modify
138138
// Version codes can be changed in updateVersionNumbers.go
139139

140-
const jsAdminVersion = 13
140+
const jsAdminVersion = 14
141141
const jsE2EVersion = 8
142142
const cssMainVersion = 5

build/go-generate/updateVersionNumbers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212
)
1313

14-
const versionJsAdmin = 13
14+
const versionJsAdmin = 14
1515
const versionJsDropzone = 5
1616
const versionJsE2EAdmin = 8
1717
const versionCssMain = 5

internal/environment/Environment.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ const DefaultPort = 53842
1616

1717
// Environment is a struct containing available env variables
1818
type Environment struct {
19-
ConfigDir string `env:"CONFIG_DIR" envDefault:"config"`
20-
ConfigFile string `env:"CONFIG_FILE" envDefault:"config.json"`
21-
ConfigPath string
22-
DataDir string `env:"DATA_DIR" envDefault:"data"`
23-
ChunkSizeMB int `env:"CHUNK_SIZE_MB" envDefault:"45"`
24-
LengthId int `env:"LENGTH_ID" envDefault:"15"`
25-
LengthHotlinkId int `env:"LENGTH_HOTLINK_ID" envDefault:"40"`
26-
MaxFileSize int `env:"MAX_FILESIZE" envDefault:"102400"` // 102400==100GB
27-
MaxMemory int `env:"MAX_MEMORY_UPLOAD" envDefault:"50"`
28-
MaxParallelUploads int `env:"MAX_PARALLEL_UPLOADS" envDefault:"3"`
29-
WebserverPort int `env:"PORT" envDefault:"53842"`
30-
MinLengthPassword int `env:"MIN_LENGTH_PASSWORD" envDefault:"8"`
31-
DisableCorsCheck bool `env:"DISABLE_CORS_CHECK" envDefault:"false"`
32-
LogToStdout bool `env:"LOG_STDOUT" envDefault:"false"`
33-
HotlinkVideos bool `env:"ENABLE_HOTLINK_VIDEOS" envDefault:"false"`
19+
ConfigDir string `env:"CONFIG_DIR" envDefault:"config"`
20+
ConfigFile string `env:"CONFIG_FILE" envDefault:"config.json"`
21+
ConfigPath string
22+
DataDir string `env:"DATA_DIR" envDefault:"data"`
23+
ChunkSizeMB int `env:"CHUNK_SIZE_MB" envDefault:"45"`
24+
LengthId int `env:"LENGTH_ID" envDefault:"15"`
25+
LengthHotlinkId int `env:"LENGTH_HOTLINK_ID" envDefault:"40"`
26+
MaxFileSize int `env:"MAX_FILESIZE" envDefault:"102400"` // 102400==100GB
27+
MaxMemory int `env:"MAX_MEMORY_UPLOAD" envDefault:"50"`
28+
MaxParallelUploads int `env:"MAX_PARALLEL_UPLOADS" envDefault:"3"`
29+
WebserverPort int `env:"PORT" envDefault:"53842"`
30+
MinLengthPassword int `env:"MIN_LENGTH_PASSWORD" envDefault:"8"`
31+
DisableCorsCheck bool `env:"DISABLE_CORS_CHECK" envDefault:"false"`
32+
LogToStdout bool `env:"LOG_STDOUT" envDefault:"false"`
33+
HotlinkVideos bool `env:"ENABLE_HOTLINK_VIDEOS" envDefault:"false"`
3434
PermRequestGrantedByDefault bool `env:"ALLOW_GUEST_UPLOADS_BY_DEFAULT" envDefault:"false"`
35-
AwsBucket string `env:"AWS_BUCKET"`
36-
AwsRegion string `env:"AWS_REGION"`
37-
AwsKeyId string `env:"AWS_KEY"`
38-
AwsKeySecret string `env:"AWS_KEY_SECRET"`
39-
AwsEndpoint string `env:"AWS_ENDPOINT"`
40-
ActiveDeprecations []deprecation.Deprecation
35+
AwsBucket string `env:"AWS_BUCKET"`
36+
AwsRegion string `env:"AWS_REGION"`
37+
AwsKeyId string `env:"AWS_KEY"`
38+
AwsKeySecret string `env:"AWS_KEY_SECRET"`
39+
AwsEndpoint string `env:"AWS_ENDPOINT"`
40+
ActiveDeprecations []deprecation.Deprecation
4141
}
4242

4343
// New parses the env variables

internal/webserver/authentication/users/Users.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ func Create(name string) (models.User, error) {
2828
newUser.GrantPermission(models.UserPermGuestUploads)
2929
}
3030
database.SaveUser(newUser, true)
31+
newUser, ok = database.GetUserByName(name)
32+
if !ok {
33+
return models.User{}, errors.New("user could not be created")
34+
}
3135
return newUser, nil
3236
}

internal/webserver/web/static/js/admin_ui_users.js

Lines changed: 96 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ function addNewUser() {
173173
apiUserCreate(editName.value.trim())
174174
.then(data => {
175175
$('#newUserModal').modal('hide');
176-
addRowUser(data.id, data.name);
176+
addRowUser(data.id, data.name, data.permissions);
177+
console.log(data);
177178
})
178179
.catch(error => {
179180
if (error.message == "duplicate") {
@@ -190,8 +191,87 @@ function addNewUser() {
190191

191192

192193

194+
const PermissionDefinitions = [
195+
{
196+
key: "UserPermGuestUploads",
197+
bit: 1 << 8,
198+
icon: "bi bi-box-arrow-in-down",
199+
title: "Create file requests",
200+
htmlId: userid => `perm_guest_upload_${userid}`,
201+
apiName: "PERM_GUEST_UPLOAD"
202+
},
203+
{
204+
key: "UserPermReplaceUploads",
205+
bit: 1 << 0,
206+
icon: "bi bi-recycle",
207+
title: "Replace own uploads",
208+
htmlId: userid => `perm_replace_${userid}`,
209+
apiName: "PERM_REPLACE"
210+
},
211+
{
212+
key: "UserPermListOtherUploads",
213+
bit: 1 << 1,
214+
icon: "bi bi-eye",
215+
title: "List other uploads",
216+
htmlId: userid => `perm_list_${userid}`,
217+
apiName: "PERM_LIST"
218+
},
219+
{
220+
key: "UserPermEditOtherUploads",
221+
bit: 1 << 2,
222+
icon: "bi bi-pencil",
223+
title: "Edit other uploads",
224+
htmlId: userid => `perm_edit_${userid}`,
225+
apiName: "PERM_EDIT"
226+
},
227+
{
228+
key: "UserPermDeleteOtherUploads",
229+
bit: 1 << 4,
230+
icon: "bi bi-trash3",
231+
title: "Delete other uploads",
232+
htmlId: userid => `perm_delete_${userid}`,
233+
apiName: "PERM_DELETE"
234+
},
235+
{
236+
key: "UserPermReplaceOtherUploads",
237+
bit: 1 << 3,
238+
icon: "bi bi-arrow-left-right",
239+
title: "Replace other uploads",
240+
htmlId: userid => `perm_replace_other_${userid}`,
241+
apiName: "PERM_REPLACE_OTHER"
242+
},
243+
{
244+
key: "UserPermManageLogs",
245+
bit: 1 << 5,
246+
icon: "bi bi-card-list",
247+
title: "Manage system logs",
248+
htmlId: userid => `perm_logs_${userid}`,
249+
apiName: "PERM_LOGS"
250+
},
251+
{
252+
key: "UserPermManageUsers",
253+
bit: 1 << 7,
254+
icon: "bi bi-people",
255+
title: "Manage users",
256+
htmlId: userid => `perm_users_${userid}`,
257+
apiName: "PERM_USERS"
258+
},
259+
{
260+
key: "UserPermManageApiKeys",
261+
bit: 1 << 6,
262+
icon: "bi bi-sliders2",
263+
title: "Manage API keys",
264+
htmlId: userid => `perm_api_${userid}`,
265+
apiName: "PERM_API"
266+
}
267+
];
268+
269+
function hasPermission(userPermissions, permissionBit) {
270+
return (userPermissions & permissionBit) !== 0;
271+
}
193272

194-
function addRowUser(userid, name) {
273+
274+
function addRowUser(userid, name, permissions) {
195275

196276
userid = sanitizeUserId(userid);
197277

@@ -260,23 +340,20 @@ function addRowUser(userid, name) {
260340
cellActions.appendChild(btnGroup);
261341

262342
// Permissions
263-
cellPermissions.innerHTML = `
264-
<i id="perm_replace_${userid}" class="bi bi-recycle perm-notgranted " title="Replace own uploads" onclick='changeUserPermission(${userid},"PERM_REPLACE", "perm_replace_${userid}");'></i>
265-
266-
<i id="perm_list_${userid}" class="bi bi-eye perm-notgranted " title="List other uploads" onclick='changeUserPermission(${userid},"PERM_LIST", "perm_list_${userid}");'></i>
267-
268-
<i id="perm_edit_${userid}" class="bi bi-pencil perm-notgranted " title="Edit other uploads" onclick='changeUserPermission(${userid},"PERM_EDIT", "perm_edit_${userid}");'></i>
269-
270-
<i id="perm_delete_${userid}" class="bi bi-trash3 perm-notgranted " title="Delete other uploads" onclick='changeUserPermission(${userid},"PERM_DELETE", "perm_delete_${userid}");'></i>
271-
272-
<i id="perm_replace_other_${userid}" class="bi bi-arrow-left-right perm-notgranted " title="Replace other uploads" onclick='changeUserPermission(${userid},"PERM_REPLACE_OTHER", "perm_replace_other_${userid}");'></i>
273-
274-
<i id="perm_logs_${userid}" class="bi bi-card-list perm-notgranted " title="Manage system logs" onclick='changeUserPermission(${userid},"PERM_LOGS", "perm_logs_${userid}");'></i>
275-
276-
<i id="perm_users_${userid}" class="bi bi-people perm-notgranted " title="Manage users" onclick='changeUserPermission(${userid},"PERM_USERS", "perm_users_${userid}");'></i>
277-
278-
<i id="perm_api_${userid}" class="bi bi-sliders2 perm-notgranted " title="Manage API keys" onclick='changeUserPermission(${userid},"PERM_API", "perm_api_${userid}");'></i>`;
279-
343+
cellPermissions.innerHTML = PermissionDefinitions.map(perm => {
344+
const granted = hasPermission(permissions, perm.bit)
345+
? "perm-granted"
346+
: "perm-notgranted";
347+
348+
const id = perm.htmlId(userid);
349+
350+
return `
351+
<i id="${id}"
352+
class="${perm.icon} ${granted}"
353+
title="${perm.title}"
354+
onclick='changeUserPermission(${userid}, "${perm.apiName}", "${id}")'>
355+
</i>`;
356+
}).join("");
280357

281358
setTimeout(() => {
282359

internal/webserver/web/static/js/min/admin.min.13.js renamed to internal/webserver/web/static/js/min/admin.min.14.js

Lines changed: 6 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/webserver/web/templates/string_constants.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Specifies the version of JS files, so that the browser doesn't
55
// use a cached version, if the file has been updated
6-
{{define "js_admin_version"}}13{{end}}
6+
{{define "js_admin_version"}}14{{end}}
77
{{define "js_dropzone_version"}}5{{end}}
88
{{define "js_e2eversion"}}8{{end}}
99
{{define "css_main"}}5{{end}}

0 commit comments

Comments
 (0)