-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #404 from iberryful/roles
feat: implement user roles
Showing
14 changed files
with
288 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
from flask import g | ||
from flask_restplus import Resource | ||
from flask import g, abort, request | ||
from flask_restplus import Resource, fields | ||
from pyinfraboxutils.ibflask import OK | ||
|
||
from pyinfraboxutils.ibrestplus import api | ||
|
||
user_role_setting_model = api.model('UserRoleUpdate', { | ||
'id': fields.String(required=True), | ||
'role': fields.String(required=True, enum=['user', 'devops', 'admin']), | ||
}) | ||
|
||
@api.route('/api/v1/admin/users', doc=False) | ||
class Users(Resource): | ||
|
||
def get(self): | ||
users = g.db.execute_many_dict(''' | ||
SELECT name, username, email, avatar_url | ||
SELECT id, name, username, email, avatar_url, role | ||
FROM "user" | ||
ORDER BY name | ||
''') | ||
|
||
return users | ||
|
||
@api.expect(user_role_setting_model, validate=True) | ||
def post(self): | ||
if g.token['user']['role'] != 'admin': | ||
abort(403, "updating user role is only allowed for admin user") | ||
body = request.get_json() | ||
if body['id'] == '00000000-0000-0000-0000-000000000000': | ||
abort(403, "can't change role for Admin") | ||
g.db.execute(''' | ||
UPDATE "user" | ||
SET role=%s | ||
WHERE id=%s | ||
''', [body['role'], body['id']]) | ||
g.db.commit() | ||
return OK("OK") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 186 additions & 44 deletions
230
src/dashboard-client/src/components/admin/AdminUsers.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
export default class User { | ||
constructor (username, avatarUrl, name, email, githubId, id) { | ||
constructor (username, avatarUrl, name, email, githubId, id, role) { | ||
this.githubRepos = [] | ||
this.username = username | ||
this.avatarUrl = avatarUrl | ||
this.name = name | ||
this.email = email | ||
this.githubId = githubId | ||
this.id = id | ||
this.role = role | ||
} | ||
|
||
hasGithubAccount () { | ||
return this.githubId != null | ||
} | ||
|
||
isAdmin () { | ||
return this.id === '00000000-0000-0000-0000-000000000000' | ||
return this.role === 'admin' | ||
} | ||
|
||
hasWriteAccess () { | ||
return this.role === 'devops' || this.isAdmin() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TYPE system_role AS ENUM ( | ||
'user', | ||
'devops', | ||
'admin' | ||
); | ||
|
||
ALTER TABLE "user" ADD COLUMN role system_role DEFAULT 'user' NOT NULL; | ||
UPDATE "user" SET role = 'admin' WHERE id = '00000000-0000-0000-0000-000000000000'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters