#18: Add/remove users from groups

This commit is contained in:
jendib 2016-03-20 17:30:36 +01:00
parent 689a4e6aae
commit ced64a5d1f
9 changed files with 94 additions and 14 deletions

View File

@ -21,7 +21,9 @@ import com.google.common.base.Strings;
import com.sismics.docs.core.dao.jpa.GroupDao;
import com.sismics.docs.core.dao.jpa.UserDao;
import com.sismics.docs.core.dao.jpa.criteria.GroupCriteria;
import com.sismics.docs.core.dao.jpa.criteria.UserCriteria;
import com.sismics.docs.core.dao.jpa.dto.GroupDto;
import com.sismics.docs.core.dao.jpa.dto.UserDto;
import com.sismics.docs.core.model.jpa.Group;
import com.sismics.docs.core.model.jpa.User;
import com.sismics.docs.core.model.jpa.UserGroup;
@ -327,7 +329,14 @@ public class GroupResource extends BaseResource {
response.add("parent", parentGroup.getName());
}
// TODO Add members
// Members
JsonArrayBuilder members = Json.createArrayBuilder();
UserDao userDao = new UserDao();
List<UserDto> userDtoList = userDao.findByCriteria(new UserCriteria().setGroupId(group.getId()), new SortCriteria(1, true));
for (UserDto userDto : userDtoList) {
members.add(userDto.getUsername());
}
response.add("members", members);
return Response.ok().entity(response.build()).build();
}

View File

@ -84,4 +84,43 @@ angular.module('docs').controller('SettingsGroupEdit', function($scope, $dialog,
});
return deferred.promise;
};
/**
* Returns a promise for typeahead user.
*/
$scope.getUserTypeahead = function($viewValue) {
var deferred = $q.defer();
Restangular.one('user')
.getList('list', {
search: $viewValue,
sort_column: 1,
asc: true
}).then(function(data) {
deferred.resolve(_.pluck(data.users, 'username'));
});
return deferred.promise;
};
/**
* Add a new member.
*/
$scope.addMember = function(member) {
$scope.member = '';
Restangular.one('group/' + $stateParams.name).put({
username: member
}).then(function() {
if ($scope.group.members.indexOf(member) === -1) {
$scope.group.members.push(member);
}
});
};
/**
* Remove a member.
*/
$scope.removeMember = function(member) {
Restangular.one('group/' + $stateParams.name, member).remove().then(function() {
$scope.group.members.splice($scope.group.members.indexOf(member), 1);
});
};
});

View File

@ -8,7 +8,10 @@ angular.module('docs').controller('SettingsUser', function($scope, $state, Resta
* Load users from server.
*/
$scope.loadUsers = function() {
Restangular.one('user/list').get().then(function(data) {
Restangular.one('user/list').get({
sort_column: 1,
asc: true
}).then(function(data) {
$scope.users = data.users;
});
};

View File

@ -5,7 +5,10 @@
*/
angular.module('docs').controller('User', function(Restangular, $scope, $state) {
// Load users
Restangular.one('user/list').get({ limit: 100 }).then(function(data) {
Restangular.one('user/list').get({
sort_column: 1,
asc: true
}).then(function(data) {
$scope.users = data.users;
});

View File

@ -7,7 +7,6 @@
</li>
</ul>
<input class="form-control" type="text" id="{{ ref }}" placeholder="Type a document title" ng-model="input" ng-disabled="ngDisabled"
autocomplete="off"
typeahead="document.title for document in getDocumentTypeahead($viewValue) | filter: $viewValue"
autocomplete="off" typeahead="document.title for document in getDocumentTypeahead($viewValue)"
typeahead-wait-ms="200" typeahead-on-select="addRelation($item)" />
</div>

View File

@ -9,7 +9,7 @@
<div class="col-sm-10">
<input required ng-maxlength="100" class="form-control" type="text" id="inputTitle"
placeholder="The nature or genre of the resource" name="title" ng-model="document.title" autocomplete="off"
typeahead="document for document in getTitleTypeahead($viewValue) | filter: $viewValue"
typeahead="document for document in getTitleTypeahead($viewValue)"
typeahead-wait-ms="200" ng-disabled="fileIsUploading" />
</div>
</div>

View File

@ -27,8 +27,8 @@
<div class="col-sm-7">
<input name="name" type="text" id="inputParent" class="form-control" autocomplete="off"
placeholder="Type a group name" ng-model="group.parent"
typeahead="group for group in getGroupTypeahead($viewValue) | filter: $viewValue"
placeholder="Search a group" ng-model="group.parent"
typeahead="group for group in getGroupTypeahead($viewValue)"
typeahead-wait-ms="200" typeahead-editable="false" />
</div>
</div>
@ -44,6 +44,30 @@
</div>
</form>
<div ng-show="isEdit()">
<h3>Members</h3>
<form class="form-horizontal" novalidate>
<div class="form-group">
<label class="col-sm-2 control-label" for="inputMember">New member</label>
<div class="col-sm-7">
<input name="member" type="text" id="inputMember" class="form-control" ng-model="member" placeholder="Search a user"
typeahead="user for user in getUserTypeahead($viewValue)" typeahead-on-select="addMember($item)"
typeahead-wait-ms="200" typeahead-editable="false"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Members</label>
<div class="col-sm-7">
<span ng-repeat="member in group.members">
<span class="btn btn-default"
ng-click="removeMember(member)">
{{ member }}
<span class="glyphicon glyphicon-remove"></span>
</span>&nbsp;
</span>
</div>
</div>
</form>
</div>
</div>

View File

@ -130,6 +130,9 @@ public class TestGroupResource extends BaseJerseyTest {
.get(JsonObject.class);
Assert.assertEquals("g12new", json.getString("name"));
Assert.assertEquals("g11", json.getString("parent"));
JsonArray members = json.getJsonArray("members");
Assert.assertEquals(1, members.size());
Assert.assertEquals("group1", members.getString(0));
// Remove group1 from g12new
json = target().path("/group/g12new/group1").request()