2015-04-08 22:43:59 +00:00
|
|
|
(function () {
|
|
|
|
|
2015-08-11 05:33:51 +00:00
|
|
|
function UserHeaderCtrl($scope, $stateParams, users) {
|
|
|
|
// Gets the currently authenticated user
|
|
|
|
users.getCurrent().then(function(payload){
|
|
|
|
$scope.user = payload.data;
|
|
|
|
});
|
|
|
|
|
2015-08-27 07:44:46 +00:00
|
|
|
$scope.number = $stateParams.number || undefined;
|
|
|
|
$scope.owner = $stateParams.owner || undefined;
|
|
|
|
$scope.name = $stateParams.name || undefined;
|
|
|
|
$scope.full_name = $scope.owner + '/' + $scope.name;
|
2015-08-11 05:33:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 23:08:20 +00:00
|
|
|
function UserLoginCtrl($scope, $window) {
|
|
|
|
// attempts to extract an error message from
|
|
|
|
// the URL hash in format #error=?
|
|
|
|
$scope.error = $window.location.hash.substr(7);
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:44:46 +00:00
|
|
|
function UserLogoutCtrl($scope, $window, $state) {
|
|
|
|
// Remove login information from the local
|
|
|
|
// storage and redirect to login page
|
|
|
|
if (localStorage.hasOwnProperty("access_token")) {
|
|
|
|
localStorage.removeItem("access_token");
|
|
|
|
}
|
|
|
|
|
|
|
|
$state.go("login", {}, {
|
|
|
|
location: "replace"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
/**
|
|
|
|
* UserCtrl is responsible for managing user settings.
|
2015-04-28 23:08:20 +00:00
|
|
|
*/
|
2015-04-13 23:33:29 +00:00
|
|
|
function UserCtrl($scope, users, tokens) {
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-04-28 23:08:20 +00:00
|
|
|
// Gets the currently authenticated user
|
2015-04-08 22:43:59 +00:00
|
|
|
users.getCurrent().then(function(payload){
|
|
|
|
$scope.user = payload.data;
|
|
|
|
});
|
2015-04-13 23:33:29 +00:00
|
|
|
|
|
|
|
// Gets the user tokens
|
|
|
|
tokens.list().then(function(payload){
|
2015-05-11 07:45:31 +00:00
|
|
|
$scope.tokens = payload.data || [];
|
2015-04-13 23:33:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$scope.newToken={Label: ""};
|
|
|
|
$scope.createToken = function(newToken) {
|
|
|
|
tokens.post(newToken).then(function(payload) {
|
|
|
|
$scope.tokens.push(payload.data);
|
|
|
|
$scope.newToken={Label: ""};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.revokeToken = function(token) {
|
|
|
|
tokens.delete(token).then(function() {
|
|
|
|
var index = $scope.tokens.indexOf(token);
|
|
|
|
$scope.tokens.splice(index, 1);
|
|
|
|
});
|
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UsersCtrl is responsible for managing user accounts.
|
|
|
|
* This part of the site is for administrators only.
|
2015-04-28 23:08:20 +00:00
|
|
|
*/
|
2015-04-08 22:43:59 +00:00
|
|
|
function UsersCtrl($scope, users) {
|
2015-07-30 02:51:55 +00:00
|
|
|
$scope.loading = true;
|
|
|
|
$scope.waiting = false;
|
|
|
|
|
2015-04-28 23:08:20 +00:00
|
|
|
// Gets the currently authenticated user
|
2015-04-08 22:43:59 +00:00
|
|
|
users.getCached().then(function(payload){
|
|
|
|
$scope.user = payload.data;
|
|
|
|
});
|
|
|
|
|
2015-07-30 02:51:55 +00:00
|
|
|
// Gets the list of all system users
|
2015-04-08 22:43:59 +00:00
|
|
|
users.list().then(function(payload){
|
2015-07-30 02:51:55 +00:00
|
|
|
$scope.loading = true;
|
2015-04-08 22:43:59 +00:00
|
|
|
$scope.users = payload.data;
|
|
|
|
});
|
|
|
|
|
2015-07-30 02:51:55 +00:00
|
|
|
$scope.add = function(event, login) {
|
|
|
|
$scope.error = undefined;
|
2015-07-30 06:45:06 +00:00
|
|
|
$scope.new_user = undefined;
|
2015-07-30 02:51:55 +00:00
|
|
|
if (event.which && event.which !== 13) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$scope.waiting = true;
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
users.post(login).then(function(payload){
|
|
|
|
$scope.users.push(payload.data);
|
2015-07-30 02:51:55 +00:00
|
|
|
$scope.search_text=undefined;
|
|
|
|
$scope.waiting = false;
|
2015-07-30 06:45:06 +00:00
|
|
|
$scope.new_user = payload.data;
|
2015-07-30 02:51:55 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
$scope.error = err;
|
|
|
|
$scope.waiting = false;
|
|
|
|
$scope.search_text = undefined;
|
2015-04-08 22:43:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.toggle = function(user) {
|
2015-07-30 06:45:06 +00:00
|
|
|
if (user.login === $scope.user.login) {
|
|
|
|
// cannot revoke admin privilege for self
|
|
|
|
$scope.error = {}; // todo display an actual error here
|
|
|
|
return;
|
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
user.admin = !user.admin;
|
|
|
|
users.put(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.remove = function(user) {
|
2015-07-30 06:45:06 +00:00
|
|
|
if (user.login === $scope.user.login) {
|
|
|
|
// cannot delete self
|
|
|
|
$scope.error = {}; // todo display an actual error here
|
|
|
|
return;
|
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
users.delete(user).then(function(){
|
2015-04-13 23:33:29 +00:00
|
|
|
var index = $scope.users.indexOf(user);
|
|
|
|
$scope.users.splice(index, 1);
|
2015-04-08 22:43:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('drone')
|
2015-08-11 05:33:51 +00:00
|
|
|
.controller('UserHeaderCtrl', UserHeaderCtrl)
|
2015-04-28 23:08:20 +00:00
|
|
|
.controller('UserLoginCtrl', UserLoginCtrl)
|
2015-08-27 07:44:46 +00:00
|
|
|
.controller('UserLogoutCtrl', UserLogoutCtrl)
|
2015-04-08 22:43:59 +00:00
|
|
|
.controller('UserCtrl', UserCtrl)
|
|
|
|
.controller('UsersCtrl', UsersCtrl);
|
2015-04-28 23:08:20 +00:00
|
|
|
})();
|