added access control to costcenter and tariff in api and admin

pull/95/MERGE
13621160019@163.com 2021-12-09 12:08:00 +08:00
parent 4d9b60af3d
commit bf6a8ba8ee
8 changed files with 158 additions and 259 deletions

View File

@ -1,158 +0,0 @@
'use strict';
app.controller('FlatController', function($scope,$uibModal,$translate, FlatService,toaster,SweetAlert) {
$scope.getAllFlats = function() {
FlatService.getAllFlats(function (response) {
if (angular.isDefined(response.status) && response.status === 200) {
$scope.flats = response.data;
} else {
$scope.flats = [];
}
});
};
$scope.addFlat = function() {
var modalInstance = $uibModal.open({
templateUrl: 'views/settings/tariff/flat.model.html',
controller: 'ModalAddFlatCtrl',
windowClass: "animated fadeIn",
resolve: {
params:function(){
return {
flats:angular.copy($scope.flats)
};
}
}
});
modalInstance.result.then(function(flat) {
FlatService.addFlat(flat, function(response) {
if (angular.isDefined(response.status) && response.status === 201) {
toaster.pop({
type: "success",
title: $translate.instant("TOASTER.SUCCESS_TITLE"),
body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("TOASTER.FLAT")}),
showCloseButton: true,
});
$scope.getAllFlats();
} else {
toaster.pop({
type: "error",
title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("TOASTER.FLAT")}),
body: $translate.instant(response.data.description),
showCloseButton: true,
});
}
});
}, function() {
});
};
$scope.editFlat=function(flat){
var modalInstance = $uibModal.open({
windowClass: "animated fadeIn",
templateUrl: 'views/settings/tariff/flat.model.html',
controller: 'ModalEditFlatCtrl',
resolve: {
params:function(){
return {
flat:angular.copy(flat),
flats:angular.copy($scope.flats)
};
}
}
});
modalInstance.result.then(function (modifiedFlat) {
FlatService.editFlat(modifiedFlat, function (response) {
if(angular.isDefined(response.status) && response.status === 200){
toaster.pop({
type: "success",
title: $translate.instant("TOASTER.SUCCESS_TITLE"),
body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("TOASTER.FLAT")}),
showCloseButton: true,
});
$scope.getAllFlats();
}else{
toaster.pop({
type: "error",
title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("TOASTER.FLAT")}),
body: $translate.instant(response.data.description),
showCloseButton: true,
});
}
});
}, function () {
//do nothing;
});
};
$scope.deleteFlat=function(flat){
SweetAlert.swal({
title: $translate.instant("SWEET.TITLE"),
text: $translate.instant("SWEET.TEXT"),
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
closeOnConfirm: true,
closeOnCancel: true },
function (isConfirm) {
if (isConfirm) {
FlatService.deleteFlat(flat, function (response) {
if (angular.isDefined(response.status) && response.status === 204) {
toaster.pop({
type: "success",
title: $translate.instant("TOASTER.SUCCESS_TITLE"),
body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("TOASTER.FLAT")}),
showCloseButton: true,
});
$scope.getAllFlats();
} else {
toaster.pop({
type: "error",
title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("TOASTER.FLAT")}),
body: $translate.instant(response.data.description),
showCloseButton: true,
});
}
});
}
});
};
$scope.getAllFlats();
});
app.controller('ModalAddFlatCtrl', function ($scope, $uibModalInstance,params) {
$scope.operation="添加";
$scope.flats=params.flats;
$scope.ok = function () {
$uibModalInstance.close($scope.flat);
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
app.controller('ModalEditFlatCtrl', function ($scope, $uibModalInstance, params) {
$scope.operation="编辑";
$scope.flat = params.flat;
$scope.flats=params.flats;
$scope.ok = function () {
$uibModalInstance.close($scope.flat);
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});

View File

@ -1,53 +0,0 @@
'use strict';
app.factory('FlatService', function($http) {
return {
getAllFlats:function(callback){
$http.get(getAPI()+'flats')
.then(function (response) {
callback(response);
}, function (response) {
callback(response);
});
},
searchFlats: function(query, callback) {
$http.get(getAPI()+'flats', { params: { q: query } })
.then(function (response) {
callback(response);
}, function (response) {
callback(response);
});
},
addFlat: function(flat, callback) {
$http.post(getAPI()+'flats',{data:flat})
.then(function (response) {
callback(response);
}, function (response) {
callback(response);
});
},
editFlat: function(flat, callback) {
$http.put(getAPI()+'flats/'+flat.id,{data:flat})
.then(function (response) {
callback(response);
}, function (response) {
callback(response);
});
},
deleteFlat: function(flat, callback) {
$http.delete(getAPI()+'flats/'+flat.id)
.then(function (response) {
callback(response);
}, function (response) {
callback(response);
});
},
getFlat: function(id, callback) {
$http.get(getAPI()+'flats/'+id)
.then(function (response) {
callback(response);
}, function (response) {
callback(response);
});
}
};
});

View File

@ -1,19 +0,0 @@
<div class="inmodal">
<div class="modal-header">
<h4 class="modal-title">{{operation | translate}}</h4>
</div>
<div class="modal-body">
<form method="get" class="form-horizontal">
<div class="form-group"><label class="col-sm-2 control-label">{{'SETTING.NAME' | translate}}</label>
<div class="col-sm-10"><input ng-model="meter.name" type="text" class="form-control"></div>
</div>
<div class="hr-line-dashed"></div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-white" ng-click="cancel()">{{'SETTING.CANCEL' | translate}}</button>
<button type="button" class="btn btn-primary" ng-click="ok()">{{'SETTING.SAVE' | translate}}</button>
</div>
</div>

View File

@ -1017,7 +1017,18 @@
"name": "POST Creat a Cost Center",
"request": {
"method": "POST",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"name\":\"动力中心\", \"external_id\":\"21829198980001\"}}"
@ -1038,7 +1049,18 @@
"name": "PUT Update a Cost Center",
"request": {
"method": "PUT",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"name\":\"动力中心2\", \"external_id\":\"21829198980002\"}}"
@ -1060,28 +1082,50 @@
"name": "DELETE a Cost Center by ID",
"request": {
"method": "DELETE",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"url": {
"raw": "{{base_url}}/costcenters/2",
"raw": "{{base_url}}/costcenters/3",
"host": [
"{{base_url}}"
],
"path": [
"costcenters",
"2"
"3"
]
}
},
"response": []
},
{
"name": "POST a Cost Center and Tariff Relation",
"name": "POST Bind Tariff to Cost Center",
"request": {
"method": "POST",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"tariff_id\":\"1\"}}"
"raw": "{\"data\":{\"tariff_id\":\"3\"}}"
},
"url": {
"raw": "{{base_url}}/costcenters/1/tariffs",
@ -1103,16 +1147,35 @@
"method": "GET",
"header": [],
"url": {
"raw": ""
"raw": "{{base_url}}/costcenters/1/tariffs",
"host": [
"{{base_url}}"
],
"path": [
"costcenters",
"1",
"tariffs"
]
}
},
"response": []
},
{
"name": "DELETE a Cost Center and Tariff Relation",
"name": "DELETE Unbind Tariff from Cost Center",
"request": {
"method": "DELETE",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"url": {
"raw": "{{base_url}}/costcenters/1/tariffs/3",
"host": [
@ -7475,10 +7538,21 @@
"response": []
},
{
"name": "POST Create a Tariff (block)",
"name": "POST Create a Tariff (Tiered)",
"request": {
"method": "POST",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"name\":\"new阶梯电价\",\"energy_category\":{\"id\":1}, \"tariff_type\":\"block\", \"unit_of_price\":\"元/千瓦时\", \"valid_from\":\"2020-01-01T00:00:00\", \"valid_through\":\"2021-01-01T00:00:00\", \"block\":[{\"start_amount\":\"0\", \"end_amount\":\"10000\", \"price\":\"0.567\"}, {\"start_amount\":\"10000\", \"end_amount\":\"30000\", \"price\":\"0.678\"}, {\"start_amount\":\"30000\", \"end_amount\":\"100000\", \"price\":\"0.789\"}]}}"
@ -7514,32 +7588,54 @@
"response": []
},
{
"name": "PUT Update a Tariff (block)",
"name": "PUT Update a Tariff (Tiered)",
"request": {
"method": "PUT",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"name\":\"new阶梯电价\",\"energy_category\":{\"id\":1}, \"tariff_type\":\"block\", \"unit_of_price\":\"元/千瓦时\", \"valid_from\":\"2020-01-01T00:00:00\", \"valid_through\":\"2021-01-01T00:00:00\", \"block\":[{\"start_amount\":\"0\", \"end_amount\":\"20000\", \"price\":\"0.567\"}, {\"start_amount\":\"20000\", \"end_amount\":\"30000\", \"price\":\"0.678\"}, {\"start_amount\":\"30000\", \"end_amount\":\"100000\", \"price\":\"0.789\"}]}}"
},
"url": {
"raw": "{{base_url}}/tariffs/18",
"raw": "{{base_url}}/tariffs/17",
"host": [
"{{base_url}}"
],
"path": [
"tariffs",
"18"
"17"
]
}
},
"response": []
},
{
"name": "POST Create a Tariff (time of use)",
"name": "POST Create a Tariff (Time of Use)",
"request": {
"method": "POST",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"name\":\"new2020分时电价1-6\",\"energy_category\":{\"id\":1}, \"tariff_type\":\"timeofuse\", \"unit_of_price\":\"元/千瓦时\", \"valid_from\":\"2020-01-01T00:00:00\", \"valid_through\":\"2020-07-01T00:00:00\", \"timeofuse\":[{\"start_time_of_day\":\"00:00:00\", \"end_time_of_day\":\"05:59:59\", \"peak_type\":\"offpeak\", \"price\":0.345}, {\"start_time_of_day\":\"06:00:00\", \"end_time_of_day\":\"07:59:59\", \"peak_type\":\"midpeak\", \"price\":0.708}, {\"start_time_of_day\":\"08:00:00\", \"end_time_of_day\":\"10:59:59\", \"peak_type\":\"onpeak\", \"price\":1.159}, {\"start_time_of_day\":\"11:00:00\", \"end_time_of_day\":\"17:59:59\", \"peak_type\":\"midpeak\", \"price\":0.708}, {\"start_time_of_day\":\"18:00:00\", \"end_time_of_day\":\"20:59:59\", \"peak_type\":\"onpeak\", \"price\":1.159}, {\"start_time_of_day\":\"21:00:00\", \"end_time_of_day\":\"21:59:59\", \"peak_type\":\"midpeak\", \"price\":0.708}, {\"start_time_of_day\":\"22:00:00\", \"end_time_of_day\":\"23:59:59\", \"peak_type\":\"offpeak\", \"price\":0.345}]}}"
@ -7560,19 +7656,30 @@
"name": "PUT Update a Tariff (time of use)",
"request": {
"method": "PUT",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"name\":\"new2020分时电价1-6\",\"energy_category\":{\"id\":1}, \"tariff_type\":\"timeofuse\", \"unit_of_price\":\"元/千瓦时\", \"valid_from\":\"2020-01-01T00:00:00\", \"valid_through\":\"2020-07-01T00:00:00\", \"timeofuse\":[{\"start_time_of_day\":\"00:00:00\", \"end_time_of_day\":\"05:59:59\", \"peak_type\":\"offpeak\", \"price\":0.456}, {\"start_time_of_day\":\"06:00:00\", \"end_time_of_day\":\"07:59:59\", \"peak_type\":\"midpeak\", \"price\":0.708}, {\"start_time_of_day\":\"08:00:00\", \"end_time_of_day\":\"10:59:59\", \"peak_type\":\"onpeak\", \"price\":1.159}, {\"start_time_of_day\":\"11:00:00\", \"end_time_of_day\":\"17:59:59\", \"peak_type\":\"midpeak\", \"price\":0.708}, {\"start_time_of_day\":\"18:00:00\", \"end_time_of_day\":\"20:59:59\", \"peak_type\":\"onpeak\", \"price\":1.159}, {\"start_time_of_day\":\"21:00:00\", \"end_time_of_day\":\"21:59:59\", \"peak_type\":\"midpeak\", \"price\":0.708}, {\"start_time_of_day\":\"22:00:00\", \"end_time_of_day\":\"23:59:59\", \"peak_type\":\"offpeak\", \"price\":0.345}]}}"
},
"url": {
"raw": "{{base_url}}/tariffs/19",
"raw": "{{base_url}}/tariffs/18",
"host": [
"{{base_url}}"
],
"path": [
"tariffs",
"19"
"18"
]
}
},
@ -7582,15 +7689,26 @@
"name": "DELETE a Tariff",
"request": {
"method": "DELETE",
"header": [],
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "text"
},
{
"key": "Token",
"value": "dca233ce58b2bf881e862e994f392dd27dd286c6135eff14da111137023b6c5625864300b269048036dcec310d4f61fa89021e5a3462512de2852493227187a0",
"type": "text"
}
],
"url": {
"raw": "{{base_url}}/tariffs/16",
"raw": "{{base_url}}/tariffs/18",
"host": [
"{{base_url}}"
],
"path": [
"tariffs",
"16"
"18"
]
}
},

View File

@ -343,20 +343,20 @@ Result
* GET All Cost Files
```bash
curl -i -X GET {{base_url}}/costfiles
curl -i -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -X GET {{base_url}}/costfiles
```
* DELETE a Cost File by ID
```bash
curl -i -X DELETE {{base_url}}/costfiles/{id}
curl -i -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -X DELETE {{base_url}}/costfiles/{id}
```
* POST Upload a Cost File
(call users login API to get 'User-UUID' and 'Token')
```bash
curl -i -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -H "Content-Type: application/TBD" -X POST -d 'file: (binary)' {{base_url}}/costfiles
curl -i -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -H "Content-Type: application/TBD" -X POST -d 'file: (binary)' {{base_url}}/costfiles
```
* GET Restore a Cost File by ID from database to disk
```bash
curl -i -X GET {{base_url}}/costfiles/{id}/restore
curl -i -H "User-UUID: dcdb67d1-6116-4987-916f-6fc6cf2bc0e4" -H "Token: GET-TOKEN-AFTER-LOGIN" -X GET {{base_url}}/costfiles/{id}/restore
```
### Data Source

View File

@ -18,6 +18,7 @@ class CostCenterCollection:
@staticmethod
def on_get(req, resp):
"""Handles GET requests"""
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor()
@ -111,6 +112,7 @@ class CostCenterItem:
@staticmethod
def on_get(req, resp, id_):
"""Handles GET requests"""
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_COST_CENTER_ID')
@ -136,6 +138,7 @@ class CostCenterItem:
@staticmethod
@user_logger
def on_delete(req, resp, id_):
"""Handles DELETE requests"""
access_control(req)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
@ -379,6 +382,7 @@ class CostCenterTariffCollection:
@staticmethod
def on_get(req, resp, id_):
"""Handles GET requests"""
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_COST_CENTER_ID')
@ -482,6 +486,7 @@ class CostCenterTariffItem:
@staticmethod
@user_logger
def on_delete(req, resp, id_, tid):
"""Handles DELETE requests"""
access_control(req)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',

View File

@ -20,6 +20,7 @@ class CostFileCollection:
@staticmethod
def on_get(req, resp):
"""Handles GET requests"""
access_control(req)
cnx = mysql.connector.connect(**config.myems_historical_db)
cursor = cnx.cursor()
@ -159,6 +160,7 @@ class CostFileItem:
@staticmethod
def on_get(req, resp, id_):
"""Handles GET requests"""
access_control(req)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400,
@ -194,6 +196,7 @@ class CostFileItem:
@staticmethod
@user_logger
def on_delete(req, resp, id_):
"""Handles DELETE requests"""
access_control(req)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
@ -245,6 +248,7 @@ class CostFileRestore:
@staticmethod
def on_get(req, resp, id_):
"""Handles GET requests"""
access_control(req)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',

View File

@ -233,6 +233,7 @@ class TariffItem:
@staticmethod
def on_get(req, resp, id_):
"""Handles GET requests"""
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_TARIFF_ID')
@ -312,6 +313,7 @@ class TariffItem:
@staticmethod
@user_logger
def on_delete(req, resp, id_):
"""Handles DELETE requests"""
access_control(req)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',