Skip to content

Commit d3aee21

Browse files
committed
Added migration for new file request user permission, added permission to API endpoint
1 parent 0826a01 commit d3aee21

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

internal/configuration/database/provider/redis/Redis.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redis
33
import (
44
"errors"
55
"fmt"
6+
"github.com/forceu/gokapi/internal/environment"
67
"github.com/forceu/gokapi/internal/helper"
78
"github.com/forceu/gokapi/internal/models"
89
redigo "github.com/gomodule/redigo/redis"
@@ -19,7 +20,7 @@ type DatabaseProvider struct {
1920
}
2021

2122
// DatabaseSchemeVersion contains the version number to be expected from the current database. If lower, an upgrade will be performed
22-
const DatabaseSchemeVersion = 5
23+
const DatabaseSchemeVersion = 6
2324

2425
// New returns an instance
2526
func New(dbConfig models.DbConnection) (DatabaseProvider, error) {
@@ -99,6 +100,20 @@ func (p DatabaseProvider) Upgrade(currentDbVersion int) {
99100
osExit(1)
100101
return
101102
}
103+
// pre multi-user
104+
if currentDbVersion < 6 {
105+
if environment.New().PermRequestGrantedByDefault {
106+
for _, user := range p.GetAllUsers() {
107+
user.GrantPermission(models.UserPermGuestUploads)
108+
p.SaveUser(user, false)
109+
}
110+
}
111+
for _, apiKey := range p.GetAllApiKeys() {
112+
if apiKey.IsSystemKey {
113+
p.DeleteApiKey(apiKey.Id)
114+
}
115+
}
116+
}
102117
}
103118

104119
const keyDbVersion = "dbversion"

internal/configuration/database/provider/sqlite/Sqlite.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"errors"
66
"fmt"
7+
"github.com/forceu/gokapi/internal/environment"
78
"os"
89
"path/filepath"
910

@@ -19,7 +20,7 @@ type DatabaseProvider struct {
1920
}
2021

2122
// DatabaseSchemeVersion contains the version number to be expected from the current database. If lower, an upgrade will be performed
22-
const DatabaseSchemeVersion = 10
23+
const DatabaseSchemeVersion = 11
2324

2425
// New returns an instance
2526
func New(dbConfig models.DbConnection) (DatabaseProvider, error) {
@@ -39,6 +40,20 @@ func (p DatabaseProvider) Upgrade(currentDbVersion int) {
3940
osExit(1)
4041
return
4142
}
43+
// pre multi-user
44+
if currentDbVersion < 11 {
45+
if environment.New().PermRequestGrantedByDefault {
46+
for _, user := range p.GetAllUsers() {
47+
user.GrantPermission(models.UserPermGuestUploads)
48+
p.SaveUser(user, false)
49+
}
50+
}
51+
for _, apiKey := range p.GetAllApiKeys() {
52+
if apiKey.IsSystemKey {
53+
p.DeleteApiKey(apiKey.Id)
54+
}
55+
}
56+
}
4257
}
4358

4459
// GetDbVersion gets the version number of the database

internal/webserver/api/routing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ func (p *paramUserModify) ProcessParameter(_ *http.Request) error {
422422
p.Permission = models.UserPermManageApiKeys
423423
case "PERM_USERS":
424424
p.Permission = models.UserPermManageUsers
425+
case "PERM_GUEST_UPLOAD":
426+
p.Permission = models.UserPermGuestUploads
425427
default:
426428
return errors.New("invalid permission")
427429
}

internal/webserver/web/static/apidocumentation/openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@
989989
"explode": false,
990990
"schema": {
991991
"type": "string",
992-
"enum": ["PERM_REPLACE", "PERM_LIST", "PERM_EDIT", "PERM_REPLACE_OTHER", "PERM_DELETE", "PERM_LOGS", "PERM_API", "PERM_USERS"]
992+
"enum": ["PERM_REPLACE", "PERM_LIST", "PERM_EDIT", "PERM_REPLACE_OTHER", "PERM_DELETE", "PERM_LOGS", "PERM_API", "PERM_USERS", "PERM_GUEST_UPLOAD"]
993993
}
994994
},
995995
{

openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@
989989
"explode": false,
990990
"schema": {
991991
"type": "string",
992-
"enum": ["PERM_REPLACE", "PERM_LIST", "PERM_EDIT", "PERM_REPLACE_OTHER", "PERM_DELETE", "PERM_LOGS", "PERM_API", "PERM_USERS"]
992+
"enum": ["PERM_REPLACE", "PERM_LIST", "PERM_EDIT", "PERM_REPLACE_OTHER", "PERM_DELETE", "PERM_LOGS", "PERM_API", "PERM_USERS", "PERM_GUEST_UPLOAD"]
993993
}
994994
},
995995
{

0 commit comments

Comments
 (0)