Skip to content

Commit

Permalink
finish role state functions
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r0x committed Jul 21, 2017
1 parent 4bbaf87 commit 7d8485d
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 55 deletions.
46 changes: 26 additions & 20 deletions app.js
Expand Up @@ -215,28 +215,15 @@ spacialistApp.service('modalFactory', ['$uibModal', function($uibModal) {
});
modalInstance.result.then(function() {}, function() {});
};
this.editRoleModal = function(onEdit, role) {
this.addRoleModal = function(onAdd, roles) {
var modalInstance = $uibModal.open({
templateUrl: 'layouts/edit-role.html',
templateUrl: 'modals/add-role.html',
controller: function($uibModalInstance) {
this.roleinfo = angular.copy(role);
this.cancel = function(result) {
$uibModalInstance.dismiss('cancel');
};
this.onEdit = function(roleinfo) {
var changes = {};
if(!role) {
changes = roleinfo;
} else {
for(var key in role) {
if(role.hasOwnProperty(key)) {
if(role[key] != roleinfo[key]) {
changes[key] = roleinfo[key];
}
}
}
}
onEdit(role, changes);
this.onAdd = function(newRole) {
onAdd(newRole, roles);
$uibModalInstance.dismiss('ok');
};
},
Expand Down Expand Up @@ -1331,7 +1318,6 @@ spacialistApp.config(function($stateProvider, $urlRouterProvider, $authProvider,
return user;
},
selectedUser: function(users, $transition$) {
// TODO open modal
return users.find(function(u) {
return u.id == $transition$.params().id;
});
Expand Down Expand Up @@ -1382,7 +1368,6 @@ spacialistApp.config(function($stateProvider, $urlRouterProvider, $authProvider,
})
.state('root.role.edit', {
url: '/edit/{id:[0-9]+}',
component: 'roleedit',
resolve: {
user: function(user) {
// TODO other access to user object?
Expand All @@ -1394,7 +1379,28 @@ spacialistApp.config(function($stateProvider, $urlRouterProvider, $authProvider,
return r.id == $transition$.params().id;
});
}
}
},
onEnter: ['role', '$state', '$uibModal', function(role, $state, $uibModal) {
$uibModal.open({
templateUrl: "modals/edit-role.html",
controller: ['$scope', 'userService', function($scope, userService) {
var orgRole = role;
$scope.editRole = angular.copy(orgRole);

$scope.cancel = function() {
$scope.$dismiss();
};

$scope.onEdit = function(editRole) {
userService.editRole(orgRole, editRole).then(function() {
$scope.$close(true);
});
};
}]
}).result.finally(function() {
$state.go('^');
});
}]
})
.state('root.bibliography', {
url: '/bibliography',
Expand Down
85 changes: 74 additions & 11 deletions app/userService.js
Expand Up @@ -60,7 +60,11 @@ spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'http

user.openAddUserDialog = function(users) {
modalFactory.addUserModal(user.addUser, users);
}
};

user.openAddRoleDialog = function(roles) {
modalFactory.addRoleModal(user.addRole, roles);
};

user.addUser = function(name, email, password, users) {
var formData = new FormData();
Expand Down Expand Up @@ -111,8 +115,50 @@ spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'http
});
};

user.openEditRoleDialog = function(role) {
modalFactory.editRoleModal(editRole, role);
user.addRole = function(role, roles) {
if(!role.name) {
var content = $translate.instant('snackbar.role.missing-name.error');
snackbarService.addAutocloseSnack(content, 'error')
return;
}
var formData = new FormData();
for(var k in role) {
if(role.hasOwnProperty(k)) {
formData.append(k, role[k]);
}
}
httpPostFactory('api/user/role', formData, function(response) {
var role = {};
for(var k in response.role) {
if(response.role.hasOwnProperty(k)) {
role[k] = response.role[k];
}
}
roles.push(role);
var content = $translate.instant('snackbar.data-stored.success');
snackbarService.addAutocloseSnack(content, 'success');
});
};

user.editRole = function(orgRole, newRole) {
var oldValues = angular.copy(orgRole);
var formData = new FormData();
for(var k in newRole) {
if(newRole.hasOwnProperty(k)) {
if(newRole[k] != oldValues[k]) {
formData.append(k, newRole[k]);
}
}
}
return httpPatchPromise.getData('api/user/role/' + orgRole.name, formData).then(function(response) {
for(var k in response.role) {
if(response.role.hasOwnProperty(k)) {
orgRole[k] = response.role[k];
}
}
var content = $translate.instant('snackbar.data-updated.success');
snackbarService.addAutocloseSnack(content, 'success');
});
};

function editRole(role, changes) {
Expand Down Expand Up @@ -152,11 +198,11 @@ spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'http
}
}

user.deleteRole = function(role) {
user.deleteRole = function(role, roles) {
httpDeleteFactory('api/user/role/' + role.id, function(response) {
if(response.error) return;
var index = user.roles.indexOf(role);
if(index > -1) user.roles.splice(index, 1);
var index = roles.indexOf(role);
if(index > -1) roles.splice(index, 1);
});
};

Expand All @@ -166,8 +212,12 @@ spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'http
if(response.error) {
var index = role.permissions.indexOf(item);
if(index > -1) role.permissions.splice(index, 1);
var content = $translate.instant('snackbar.data-updated.error');
snackbarService.addAutocloseSnack(content, 'error');
return;
}
var content = $translate.instant('snackbar.data-updated.success');
snackbarService.addAutocloseSnack(content, 'success');
});
};

Expand All @@ -191,21 +241,34 @@ spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'http
});
};

user.addUserRole = function(item, user_id) {
user.addUserRole = function(item, user) {
var formData = new FormData();
formData.append('role_id', item.id);
httpPatchFactory('api/user/' + user_id + '/attachRole', formData, function(response) {
// TODO only remove/add role if function returns no error
httpPatchFactory('api/user/' + user.id + '/attachRole', formData, function(response) {
// if an error occurs, remove added role
if(response.error) {
var index = user.roles.indexOf(item);
if(index > -1) user.roles.splice(index, 1);
var content = $translate.instant('snackbar.data-updated.error');
snackbarService.addAutocloseSnack(content, 'error');
return;
}
var content = $translate.instant('snackbar.data-updated.success');
snackbarService.addAutocloseSnack(content, 'success');
});
};

user.removeUserRole = function(item, user_id) {
user.removeUserRole = function(item, user) {
var formData = new FormData();
formData.append('role_id', item.id);
httpPatchFactory('api/user/' + user_id + '/detachRole', formData, function(response) {
// TODO only remove/add role if function returns no error
// if an error occurs, readd removed role
if(response.error) {
user.roles.push(item);
var content = $translate.instant('snackbar.data-updated.error');
snackbarService.addAutocloseSnack(content, 'error');
return;
}
var content = $translate.instant('snackbar.data-updated.success');
snackbarService.addAutocloseSnack(content, 'success');
});
Expand Down
10 changes: 2 additions & 8 deletions components/role.js
Expand Up @@ -5,12 +5,6 @@ spacialistApp.component('role', {
permissionsPerRole: '<',
user: '<'
},
templateUrl: 'roles.html'
});

spacialistApp.component('roleedit', {
bindings: {
user: '<',
role: '<'
}
templateUrl: 'role.html',
controller: 'userCtrl'
});
17 changes: 7 additions & 10 deletions controllers/userCtrl.js
@@ -1,5 +1,6 @@
spacialistApp.controller('userCtrl', ['$scope', 'userService', 'mainService', '$state', 'modalFactory', function($scope, userService, mainService, $state, modalFactory) {
var localUsers = this.users;
var localRoles = this.roles;

$scope.loginError = userService.loginError;
$scope.deleteUser = userService.deleteUser;
Expand Down Expand Up @@ -36,23 +37,19 @@ spacialistApp.controller('userCtrl', ['$scope', 'userService', 'mainService', '$
};

$scope.deleteRole = function(role) {
userService.deleteRole(role);
userService.deleteRole(role, localRoles);
};

$scope.openAddRoleDialog = function() {
userService.openEditRoleDialog();
userService.openAddRoleDialog(localRoles);
};

$scope.openEditRoleDialog = function(role) {
userService.openEditRoleDialog(role);
$scope.addUserRole = function(item, user) {
userService.addUserRole(item, user);
};

$scope.addUserRole = function(item, user_id) {
userService.addUserRole(item, user_id);
};

$scope.removeUserRole = function(item, user_id) {
userService.removeUserRole(item, user_id);
$scope.removeUserRole = function(item, user) {
userService.removeUserRole(item, user);
};

$scope.openAddUserDialog = function() {
Expand Down
2 changes: 2 additions & 0 deletions l10n/de.json
Expand Up @@ -164,6 +164,7 @@
"user.edit.heading": "Benutzer Bearbeiten",
"user.edit.new-password": "Neues Passwort",
"role.edit.heading": "Rolle Bearbeiten",
"role.add.heading": "Neue Rolle anlegen",
"map-marker.not-linked-info": "Diese Markierung ist mit keinem Kontext verknüpft.",
"map-marker.how-to-link": "Um eine Verknüpfung herzustellen, wählen Sie einen Kontext aus dem Baum. Dann erscheint hier ein Knopf zum Verknüpfen der Markierung mit dem gewählten Kontext.",
"map-marker.wrong-type.info": "Das ausgewählte Geodaten-Objekt kann nicht mit dem ausgewählten Kontext verknüpft werden, weil die erlaubten Geometrietypen ({{gtype}} und {{ctype}}) nicht übereinstimmen.",
Expand Down Expand Up @@ -241,6 +242,7 @@
"snackbar.image-deleted.success": "Datei gelöscht.",
"snackbar.bibtex-upload.success": "{{ cnt }} Einträge hinzugefügt.",
"snackbar.layer-update.success": "Ebene erfolgreich aktualisiert.",
"snackbar.role.missing-name.error": "Rolle kann ohne Name nicht angelegt werden.",

"dates_list": "Zeitangaben",
"map": "Karte",
Expand Down
2 changes: 2 additions & 0 deletions l10n/en.json
Expand Up @@ -153,6 +153,7 @@
"user.edit.heading": "Edit User",
"user.edit.new-password": "New Password",
"role.edit.heading": "Edit Role",
"role.add.heading": "Add New Role",
"map-marker.not-linked-info": "This marker is not linked to any context.",
"map-marker.how-to-link": "To link the marker with a context, select a context in the tree on the left-hand side.",
"map-marker.wrong-type.info": "The selected geodata object cannot be linked to the selected context, because their allowed geometry types ({{gtype}} and {{ctype}}) does not match.",
Expand Down Expand Up @@ -230,6 +231,7 @@
"snackbar.image-deleted.success": "File deleted.",
"snackbar.bibtex-upload.success": "{{ cnt }} entries added.",
"snackbar.layer-update.success": "Layer successfully updated.",
"snackbar.role.missing-name.error": "Cannot create role without name.",

"dates_list": "Dates list",
"map": "Map",
Expand Down
10 changes: 5 additions & 5 deletions layouts/edit-role.html → modals/add-role.html
@@ -1,5 +1,5 @@
<div class="modal-header">
<h4>{{'role.edit.heading'|translate}}</h4>
<h4>{{'role.add.heading'|translate}}</h4>
</div>
<div class="modal-body">
<div class="col-md-12">
Expand All @@ -9,30 +9,30 @@ <h4>{{'role.edit.heading'|translate}}</h4>
{{'role.name'|translate}}:
</label>
<div class="col-md-9">
<input class="form-control" type="text" id="name" ng-model="mc.roleinfo.name" />
<input class="form-control" type="text" id="name" ng-model="mc.newRole.name" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="display_name">
{{'role.display_name'|translate}}
</label>
<div class="col-md-9">
<input class="form-control" type="text" id="display_name" ng-model="mc.roleinfo.display_name" />
<input class="form-control" type="text" id="display_name" ng-model="mc.newRole.display_name" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="description">
{{'role.description'|translate}}:
</label>
<div class="col-md-9">
<input class="form-control" type="text" id="description" ng-model="mc.roleinfo.description" />
<input class="form-control" type="text" id="description" ng-model="mc.newRole.description" />
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" ng-click="mc.onEdit(mc.roleinfo)">{{ 'Save' | translate }}</button>
<button type="button" class="btn btn-success" ng-click="mc.onAdd(mc.newRole)">{{ 'Save' | translate }}</button>
<button type="button" class="btn btn-danger"
ng-click="mc.cancel()">{{ 'cancel' | translate }}</button>
</div>
40 changes: 40 additions & 0 deletions modals/edit-role.html
@@ -0,0 +1,40 @@
<div class="modal-header">
<h4>{{'role.edit.heading'|translate}}</h4>
</div>
<div class="modal-body">
<div class="col-md-12">
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-md-3" for="name">
{{'role.name'|translate}}:
</label>
<div class="col-md-9">
<p class="form-control-static" type="text" id="name">
{{ editRole.name }}
</p>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="display_name">
{{'role.display_name'|translate}}
</label>
<div class="col-md-9">
<input class="form-control" type="text" id="display_name" ng-model="editRole.display_name" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="description">
{{'role.description'|translate}}:
</label>
<div class="col-md-9">
<input class="form-control" type="text" id="description" ng-model="editRole.description" />
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" ng-click="onEdit(editRole)">{{ 'Save' | translate }}</button>
<button type="button" class="btn btn-danger"
ng-click="cancel()">{{ 'cancel' | translate }}</button>
</div>
File renamed without changes.
2 changes: 1 addition & 1 deletion user.html
Expand Up @@ -36,7 +36,7 @@
</button>
</td>
<td ng-if="$ctrl.user.permissions.add_remove_role">
<ui-select multiple ng-model="user.roles" close-on-select="false" name="user_roles_{{ user.id }}" id="user_roles_{{ user.id }}" on-select="addUserRole($item, user.id)" on-remove="removeUserRole($item, user.id)">
<ui-select multiple ng-model="user.roles" close-on-select="false" name="user_roles_{{ user.id }}" id="user_roles_{{ user.id }}" on-select="addUserRole($item, user)" on-remove="removeUserRole($item, user)">
<ui-select-match>{{ $item.display_name }}</ui-select-match>
<ui-select-choices repeat="role in $ctrl.roles | filter:$select.search">
{{ role.display_name }}
Expand Down

0 comments on commit 7d8485d

Please sign in to comment.