Skip to content

Commit

Permalink
Merge pull request #28 from revalo/master
Browse files Browse the repository at this point in the history
Add make admin button
  • Loading branch information
jlin816 authored Aug 9, 2018
2 parents 6146b1a + aebf465 commit bf6e69b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/client/src/services/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ angular.module('reg')
return $http.post(base + id + '/checkout');
},

makeAdmin: function(id){
return $http.post(base + id + '/makeadmin');
},

removeAdmin: function(id){
return $http.post(base + id + '/removeadmin');
},
};
}
]);
32 changes: 32 additions & 0 deletions app/client/views/admin/users/adminUsersCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,38 @@ angular.module('reg')

};

$scope.toggleAdmin = function($event, user, index) {
$event.stopPropagation();

if (!user.admin){
swal({
title: "Whoa, wait a minute!",
text: "You are about make " + user.profile.name + " an admin!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, make them an admin.",
closeOnConfirm: false
},
function(){
UserService
.makeAdmin(user._id)
.success(function(user){
$scope.users[index] = user;
swal("Made", user.profile.name + ' an admin.', "success");
});
}
);
} else {
UserService
.removeAdmin(user._id)
.success(function(user){
$scope.users[index] = user;
swal("Removed", user.profile.name + ' as admin', "success");
});
}
};

function formatTime(time){
if (time) {
return moment(time).format('MMMM Do YYYY, h:mm:ss a');
Expand Down
14 changes: 14 additions & 0 deletions app/client/views/admin/users/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ <h3> {{section.title}} </h3>

</button>

<button
ng-click="toggleAdmin($event, user, $index)"
class="ui circular mini basic green icon button">

<i
ng-if="!user.admin"
class="spy outline icon"></i>

<i
ng-if="user.admin"
class="green spy icon"></i>

</button>

</td>
</tr>
</tbody>
Expand Down
43 changes: 43 additions & 0 deletions app/server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,49 @@ UserController.checkOutById = function(id, user, callback){
callback);
};

/**
* [ADMIN ONLY]
*
* Make user an admin
* @param {String} userId User id of the user being made admin
* @param {String} user User making this person admin
* @param {Function} callback args(err, user)
*/
UserController.makeAdminById = function(id, user, callback){
User.findOneAndUpdate({
_id: id,
verified: true
},{
$set: {
'admin': true
}
}, {
new: true
},
callback);
};

/**
* [ADMIN ONLY]
*
* Make user an admin
* @param {String} userId User id of the user being made admin
* @param {String} user User making this person admin
* @param {Function} callback args(err, user)
*/
UserController.removeAdminById = function(id, user, callback){
User.findOneAndUpdate({
_id: id,
verified: true
},{
$set: {
'admin': false
}
}, {
new: true
},
callback);
};

/**
* [ADMIN ONLY]
Expand Down
18 changes: 18 additions & 0 deletions app/server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ module.exports = function(router) {
UserController.checkOutById(id, user, defaultResponse(req, res));
});

/**
* Make user an admin
*/
router.post('/users/:id/makeadmin', isAdmin, function(req, res){
var id = req.params.id;
var user = req.user;
UserController.makeAdminById(id, user, defaultResponse(req, res));
});

/**
* Demote user
*/
router.post('/users/:id/removeadmin', isAdmin, function(req, res){
var id = req.params.id;
var user = req.user;
UserController.removeAdminById(id, user, defaultResponse(req, res));
});


// ---------------------------------------------
// Settings [ADMIN ONLY!]
Expand Down

0 comments on commit bf6e69b

Please sign in to comment.