Skip to content

Commit

Permalink
Merge pull request #68 from EdSabino/fixUserPermissoes
Browse files Browse the repository at this point in the history
Fix user permissoes
  • Loading branch information
mateusKoppe authored Oct 7, 2019
2 parents e06f2ab + 94766b7 commit b72a39d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion App/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function index($request, $response, $args) {

public function update($request, $response, $args) {

AuthHelper::allowAuthenticated();
AuthHelper::restrictToPermission(User::USER_LEVEL_ADMIN, "JSON");

if(isset($_REQUEST['type']) && isset($args['id'])){
$userNewRole = $_REQUEST['type'];
Expand Down
11 changes: 9 additions & 2 deletions App/Helpers/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@ public static function allowAuthenticated() {
}
}

public static function restrictToPermission($level) {
public static function restrictToPermission($level, $type = "HTML") {
$title = '401';
$isAuthenticated = AuthHelper::isAuthenticated();
$user = AuthHelper::getAuthenticatedUser();
if(!$isAuthenticated || !$user->isLevel($level)) {
$isAdmin = !$isAuthenticated || !$user->isLevel($level);
if($isAdmin && $type == 'HTML') {
$data = compact(['user', 'title']);
View::render('layout/admin/header', $data);
View::render('errors/401', $data);
View::render('layout/admin/footer', $data);
exit();
} else if ($isAdmin && $type == 'JSON') {
header("message: 'Usuário não autorizado!'");
header("Content-Type: text/html; charset=UTF-8")
http_response_code(401);
echo json_encode($obj);
exit();
}
}

Expand Down
2 changes: 1 addition & 1 deletion App/Views/auth/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@
<?php endforeach;?>
</div>
</div>
<script type="text/javascript">
<script type="text/javascript" content="text/html; charset=utf-8">
SAC.userPermission();
</script>
6 changes: 2 additions & 4 deletions public/js/admin/sac.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,13 @@ var SAC = new function() {
'type' : permission
},
success : function(data, textStatus, request) {

let colorClass = $(card).attr('class');
removeColorClass(colorClass, card);
$(card).addClass(colorClasses[permission - 1]);
toastr.success(request.getResponseHeader('message'), {timeOut : 30, extendedTimeOut : 60});

},
error : function(request, textStatus, errorThrown) {
toastr.danger(request.getResponseHeader('message'), {timeOut : 30, extendedTimeOut : 60});
toastr.error(request.getResponseHeader('message'), {timeOut : 30, extendedTimeOut : 60});
}
});
});
Expand All @@ -110,4 +108,4 @@ var SAC = new function() {
$("input[name=price]").maskMoney({prefix:'R$ ', allowNegative: false, thousands:'.', decimal:',', affixesStay: false});
$("input[name=cpf]").mask('000.000.000-00');
}
};
};

0 comments on commit b72a39d

Please sign in to comment.