contact of access_control

pull/83/head
tianlinzhong 2021-11-29 18:48:29 +08:00
parent dfec9a8fc0
commit 9304225419
5 changed files with 66 additions and 21 deletions

View File

@ -1,8 +1,8 @@
'use strict'; 'use strict';
app.controller('ContactController', function($scope, $translate,$uibModal, ContactService,toaster,SweetAlert) { app.controller('ContactController', function($scope, $window, $translate,$uibModal, ContactService,toaster,SweetAlert) {
$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
$scope.getAllContacts = function() { $scope.getAllContacts = function() {
ContactService.getAllContacts(function (response) { ContactService.getAllContacts(function (response) {
if (angular.isDefined(response.status) && response.status === 200) { if (angular.isDefined(response.status) && response.status === 200) {
@ -28,7 +28,8 @@ app.controller('ContactController', function($scope, $translate,$uibModal, Conta
} }
}); });
modalInstance.result.then(function(contact) { modalInstance.result.then(function(contact) {
ContactService.addContact(contact, function(response) { let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
ContactService.addContact(contact, 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",
@ -67,7 +68,8 @@ app.controller('ContactController', function($scope, $translate,$uibModal, Conta
}); });
modalInstance.result.then(function (modifiedContact) { modalInstance.result.then(function (modifiedContact) {
ContactService.editContact(modifiedContact, function (response) { let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
ContactService.editContact(modifiedContact, 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",
@ -103,7 +105,8 @@ app.controller('ContactController', function($scope, $translate,$uibModal, Conta
closeOnCancel: true }, closeOnCancel: true },
function (isConfirm) { function (isConfirm) {
if (isConfirm) { if (isConfirm) {
ContactService.deleteContact(contact, function (response) { let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
ContactService.deleteContact(contact, 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

@ -17,24 +17,24 @@ app.factory('ContactService', function($http) {
callback(response); callback(response);
}); });
}, },
addContact: function(contact, callback) { addContact: function(contact, headers, callback) {
$http.post(getAPI()+'contacts',{data:contact}) $http.post(getAPI()+'contacts',{data:contact}, {headers})
.then(function (response) { .then(function (response) {
callback(response); callback(response);
}, function (response) { }, function (response) {
callback(response); callback(response);
}); });
}, },
editContact: function(contact, callback) { editContact: function(contact, headers, callback) {
$http.put(getAPI()+'contacts/'+contact.id,{data:contact}) $http.put(getAPI()+'contacts/'+contact.id,{data:contact}, {headers})
.then(function (response) { .then(function (response) {
callback(response); callback(response);
}, function (response) { }, function (response) {
callback(response); callback(response);
}); });
}, },
deleteContact: function(contact, callback) { deleteContact: function(contact, headers, callback) {
$http.delete(getAPI()+'contacts/'+contact.id) $http.delete(getAPI()+'contacts/'+contact.id, {headers})
.then(function (response) { .then(function (response) {
callback(response); callback(response);
}, function (response) { }, function (response) {

View File

@ -1,6 +1,6 @@
{ {
"info": { "info": {
"_postman_id": "1a6c20d8-4d7a-49a1-a5e9-3d4261ba0505", "_postman_id": "92712647-82fc-4cf2-acae-25fb13badd9e",
"name": "MyEMS", "name": "MyEMS",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
}, },
@ -634,7 +634,20 @@
"name": "POST Create New Contact", "name": "POST Create New Contact",
"request": { "request": {
"method": "POST", "method": "POST",
"header": [], "header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"description": "Any admin users' UUID",
"type": "text"
},
{
"key": "Token",
"value": "e3e753d078585ff791e8a899ad4bf3cc3673912061d5b6ab5119ed3ac713895ae1fc5f90f8d4a7942b1b139774b4db82e6702593b9a453a82982fdae47edac34",
"description": "Login to get a valid token",
"type": "text"
}
],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\"data\":{\"name\":\"albert\", \"email\":\"albert@myems.io\", \"phone\":\"+8613888888888\", \"description\":\"contact description\"}}" "raw": "{\"data\":{\"name\":\"albert\", \"email\":\"albert@myems.io\", \"phone\":\"+8613888888888\", \"description\":\"contact description\"}}"
@ -655,7 +668,20 @@
"name": "PUT Update a Contact", "name": "PUT Update a Contact",
"request": { "request": {
"method": "PUT", "method": "PUT",
"header": [], "header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"description": "Any admin users' UUID",
"type": "text"
},
{
"key": "Token",
"value": "e3e753d078585ff791e8a899ad4bf3cc3673912061d5b6ab5119ed3ac713895ae1fc5f90f8d4a7942b1b139774b4db82e6702593b9a453a82982fdae47edac34",
"description": "Login to get a valid token",
"type": "text"
}
],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\"data\":{\"name\":\"albert\", \"email\":\"albert@myems.io\", \"phone\":\"+8613888888899\", \"description\":\"contact description\"}}" "raw": "{\"data\":{\"name\":\"albert\", \"email\":\"albert@myems.io\", \"phone\":\"+8613888888899\", \"description\":\"contact description\"}}"
@ -677,7 +703,20 @@
"name": "DELETE a Contact by ID", "name": "DELETE a Contact by ID",
"request": { "request": {
"method": "DELETE", "method": "DELETE",
"header": [], "header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"description": "Any admin users' UUID",
"type": "text"
},
{
"key": "Token",
"value": "e3e753d078585ff791e8a899ad4bf3cc3673912061d5b6ab5119ed3ac713895ae1fc5f90f8d4a7942b1b139774b4db82e6702593b9a453a82982fdae47edac34",
"description": "Login to get a valid token",
"type": "text"
}
],
"url": { "url": {
"raw": "{{base_url}}/contacts/2", "raw": "{{base_url}}/contacts/2",
"host": [ "host": [

View File

@ -269,15 +269,15 @@ curl -i -X GET {{base_url}}/contacts
``` ```
* DELETE Contact by ID * DELETE Contact by ID
```bash ```bash
curl -i -X DELETE {{base_url}}/contacts/{id} curl -i -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -X DELETE {{base_url}}/contacts/{id}
``` ```
* POST Create a New Contact * POST Create a New Contact
```bash ```bash
curl -i -H "Content-Type: application/json" -X POST -d '{"data":{"name":"albert", "email":"albert@myems.io", "phone":"+8613888888888", "description":"contact description"}}' {{base_url}}/contacts 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":"albert", "email":"albert@myems.io", "phone":"+8613888888888", "description":"contact description"}}' {{base_url}}/contacts
``` ```
* PUT Update a Contact * PUT Update a Contact
```bash ```bash
curl -i -H "Content-Type: application/json" -X PUT -d '{"data":{"name":"albert", "email":"albert@myems.io", "phone":"+8613888888899", "description":"contact description"}}' {{base_url}}/contacts/{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":"albert", "email":"albert@myems.io", "phone":"+8613888888899", "description":"contact description"}}' {{base_url}}/contacts/{id}
``` ```
### Cost Center ### Cost Center

View File

@ -4,7 +4,7 @@ import mysql.connector
import config import config
import uuid import uuid
import re import re
from core.useractivity import user_logger from core.useractivity import user_logger, access_control
class ContactCollection: class ContactCollection:
@ -48,6 +48,7 @@ class ContactCollection:
@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')
except Exception as ex: except Exception as ex:
@ -160,6 +161,7 @@ class ContactItem:
@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_CONTACT_ID') description='API.INVALID_CONTACT_ID')
@ -236,6 +238,7 @@ class ContactItem:
@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')
except Exception as ex: except Exception as ex: