added access_control to privilege

pull/87/head
Caozhenhui 2021-12-02 10:43:32 +08:00
parent 213f669924
commit 8dd998ed0c
4 changed files with 21 additions and 13 deletions

View File

@ -1,11 +1,13 @@
'use strict'; 'use strict';
app.controller('PrivilegeController', function ($scope, app.controller('PrivilegeController', function ($scope,
$window,
$uibModal, $uibModal,
PrivilegeService, PrivilegeService,
toaster, toaster,
$translate, $translate,
SweetAlert) { SweetAlert) {
$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
$scope.getAllPrivileges = function () { $scope.getAllPrivileges = function () {
PrivilegeService.getAllPrivileges(function (response) { PrivilegeService.getAllPrivileges(function (response) {
if (angular.isDefined(response.status) && response.status === 200) { if (angular.isDefined(response.status) && response.status === 200) {
@ -31,7 +33,8 @@ app.controller('PrivilegeController', function ($scope,
} }
}); });
modalInstance.result.then(function (privilege) { modalInstance.result.then(function (privilege) {
PrivilegeService.addPrivilege(privilege, function (response) { let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
PrivilegeService.addPrivilege(privilege, headers, function (response) {
if (angular.isDefined(response.status) && response.status === 201) { if (angular.isDefined(response.status) && response.status === 201) {
toaster.pop({ toaster.pop({
type: "success", type: "success",
@ -70,7 +73,8 @@ app.controller('PrivilegeController', function ($scope,
}); });
modalInstance.result.then(function (modifiedPrivilege) { modalInstance.result.then(function (modifiedPrivilege) {
PrivilegeService.editPrivilege(modifiedPrivilege, function (response) { let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
PrivilegeService.editPrivilege(modifiedPrivilege, headers, function (response) {
if (angular.isDefined(response.status) && response.status === 200) { if (angular.isDefined(response.status) && response.status === 200) {
toaster.pop({ toaster.pop({
type: "success", type: "success",
@ -107,7 +111,8 @@ app.controller('PrivilegeController', function ($scope,
}, },
function (isConfirm) { function (isConfirm) {
if (isConfirm) { if (isConfirm) {
PrivilegeService.deletePrivilege(privilege, function (response) { let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
PrivilegeService.deletePrivilege(privilege, headers, function (response) {
if (angular.isDefined(response.status) && response.status === 204) { if (angular.isDefined(response.status) && response.status === 204) {
toaster.pop({ toaster.pop({
type: "success", type: "success",

View File

@ -9,24 +9,24 @@ app.factory('PrivilegeService', function($http) {
callback(response); callback(response);
}); });
}, },
addPrivilege: function(privilege, callback) { addPrivilege: function(privilege, headers, callback) {
$http.post(getAPI()+'privileges',{data:privilege}) $http.post(getAPI()+'privileges', {headers}, {data:privilege})
.then(function (response) { .then(function (response) {
callback(response); callback(response);
}, function (response) { }, function (response) {
callback(response); callback(response);
}); });
}, },
editPrivilege: function(privilege, callback) { editPrivilege: function(privilege, headers, callback) {
$http.put(getAPI()+'privileges/'+privilege.id,{data:privilege}) $http.put(getAPI()+'privileges/'+privilege.id, {headers}, {data:privilege})
.then(function (response) { .then(function (response) {
callback(response); callback(response);
}, function (response) { }, function (response) {
callback(response); callback(response);
}); });
}, },
deletePrivilege: function(privilege, callback) { deletePrivilege: function(privilege, headers, callback) {
$http.delete(getAPI()+'privileges/'+privilege.id) $http.delete(getAPI()+'privileges/'+privilege.id, {headers})
.then(function (response) { .then(function (response) {
callback(response); callback(response);
}, function (response) { }, function (response) {

View File

@ -1251,15 +1251,15 @@ curl -i -X GET {{base_url}}/privileges
``` ```
* DELETE Privilege by ID * DELETE Privilege by ID
```bash ```bash
curl -i -X DELETE {{base_url}}/privileges/{id} curl -i -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -X DELETE {{base_url}}/privileges/{id}
``` ```
* POST New Privilege * POST New Privilege
```bash ```bash
curl -i -H "Content-Type: application/json" -X POST -d '{"data":{"name":"superusers","data":"{\"spaces\":[1,2,3,5]}"}}' {{base_url}}/privileges curl -i -H "Content-Type: application/json" -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -X POST -d '{"data":{"name":"superusers","data":"{\"spaces\":[1,2,3,5]}"}}' {{base_url}}/privileges
``` ```
* PUT Privilege * PUT Privilege
```bash ```bash
curl -i -H "Content-Type: application/json" -X PUT -d '{"data":{"name":"superusers", "data":"{\"spaces\":[1, 3]}"}}' {{base_url}}/privileges/{id} curl -i -H "Content-Type: application/json" -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -X PUT -d '{"data":{"name":"superusers", "data":"{\"spaces\":[1, 3]}"}}' {{base_url}}/privileges/{id}
``` ```
### Rule ### Rule

View File

@ -2,7 +2,7 @@ import falcon
import simplejson as json import simplejson as json
import mysql.connector import mysql.connector
import config import config
from core.useractivity import user_logger from core.useractivity import user_logger, access_control
class PrivilegeCollection: class PrivilegeCollection:
@ -42,6 +42,7 @@ class PrivilegeCollection:
@user_logger @user_logger
def on_post(req, resp): def on_post(req, resp):
"""Handles POST requests""" """Handles POST requests"""
access_control(req)
try: try:
raw_json = req.stream.read().decode('utf-8') raw_json = req.stream.read().decode('utf-8')
new_values = json.loads(raw_json) new_values = json.loads(raw_json)
@ -101,6 +102,7 @@ class PrivilegeItem:
@staticmethod @staticmethod
@user_logger @user_logger
def on_delete(req, resp, id_): def on_delete(req, resp, id_):
access_control(req)
if not id_.isdigit() or int(id_) <= 0: if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_PRIVILEGE_ID') description='API.INVALID_PRIVILEGE_ID')
@ -142,6 +144,7 @@ class PrivilegeItem:
@user_logger @user_logger
def on_put(req, resp, id_): def on_put(req, resp, id_):
"""Handles PUT requests""" """Handles PUT requests"""
access_control(req)
try: try:
raw_json = req.stream.read().decode('utf-8') raw_json = req.stream.read().decode('utf-8')
new_values = json.loads(raw_json) new_values = json.loads(raw_json)