From 75b9f3c4a0605873374716c690312a80f179b3ed Mon Sep 17 00:00:00 2001 From: kiran kumar Date: Fri, 5 Dec 2014 21:29:50 +0530 Subject: [PATCH] Added delete a user service ,Added remove icon and refresh the user list after remove --- .../codebrag/dao/user/SQLUserDAO.scala | 13 +++++++++--- .../codebrag/dao/user/UserDAO.scala | 3 +++ .../codebrag/rest/UsersServlet.scala | 6 +++++- .../user/ModifyUserDetailsUseCase.scala | 4 ++++ .../app/scripts/users/manageUsersPopupCtrl.js | 20 +++++++++++++++++++ .../app/scripts/users/userMgmtService.js | 7 ++++++- codebrag-ui/app/views/popups/manageUsers.html | 8 ++++++-- 7 files changed, 54 insertions(+), 7 deletions(-) diff --git a/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/SQLUserDAO.scala b/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/SQLUserDAO.scala index aa11fa3e..3d05cd6a 100644 --- a/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/SQLUserDAO.scala +++ b/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/SQLUserDAO.scala @@ -31,7 +31,7 @@ class SQLUserDAO(val database: SQLDatabase) extends UserDAO with SQLUserSchema { } def findById(userId: ObjectId) = findOneWhere(_.id === userId) - + def findByEmail(email: String) = findOneWhere(_.emailLowerCase === email.toLowerCase) def findByLowerCasedLogin(login: String) = db.withTransaction { implicit session => @@ -124,7 +124,7 @@ class SQLUserDAO(val database: SQLDatabase) extends UserDAO with SQLUserSchema { } yield (u, a, s, l) userQuery.firstOption.map(queryUserAliases).map(untuple) } - + private def queryUserAliases(tuple: (UserTuple, SQLAuth, SQLSettings, SQLLastNotif))(implicit session: Session) = { val userId = tuple._1._1 val aliases = userAliases.where(_.userId === userId).list() @@ -135,4 +135,11 @@ class SQLUserDAO(val database: SQLDatabase) extends UserDAO with SQLUserSchema { val aliases = userAliases.where(_.userId === tuple._1).list() (tuple._1, tuple._2, tuple._3, tuple._4, UserAliases(aliases.map(_.toUserAlias))) } -} \ No newline at end of file + + def delete(userId: ObjectId) { + db.withTransaction { implicit session => + users.filter(_.id === userId).delete + } + } + +} diff --git a/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/UserDAO.scala b/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/UserDAO.scala index 2f228520..6b2b30b6 100644 --- a/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/UserDAO.scala +++ b/codebrag-dao/src/main/scala/com/softwaremill/codebrag/dao/user/UserDAO.scala @@ -40,4 +40,7 @@ trait UserDAO { def countAll(): Long def countAllActive(): Long + + def delete(userId: ObjectId) + } diff --git a/codebrag-rest/src/main/scala/com/softwaremill/codebrag/rest/UsersServlet.scala b/codebrag-rest/src/main/scala/com/softwaremill/codebrag/rest/UsersServlet.scala index 2ad75fa5..c22a1cf5 100644 --- a/codebrag-rest/src/main/scala/com/softwaremill/codebrag/rest/UsersServlet.scala +++ b/codebrag-rest/src/main/scala/com/softwaremill/codebrag/rest/UsersServlet.scala @@ -34,8 +34,12 @@ class UsersServlet( case _ => scalatra.Ok() } } - +delete("/:id") { + haltIfNotAuthenticated() + modifyUserUseCase.delete(new ObjectId(params("id"))) + } } + object UsersServlet { val MappingPath = "users" diff --git a/codebrag-service/src/main/scala/com/softwaremill/codebrag/usecases/user/ModifyUserDetailsUseCase.scala b/codebrag-service/src/main/scala/com/softwaremill/codebrag/usecases/user/ModifyUserDetailsUseCase.scala index fbe9e468..27f29f3b 100644 --- a/codebrag-service/src/main/scala/com/softwaremill/codebrag/usecases/user/ModifyUserDetailsUseCase.scala +++ b/codebrag-service/src/main/scala/com/softwaremill/codebrag/usecases/user/ModifyUserDetailsUseCase.scala @@ -51,4 +51,8 @@ class ModifyUserDetailsUseCase(protected val userDao: UserDAO) extends Logging { validate(checkUserActive, changeOwnFlagsCheck) } + def delete(userId: ObjectId) = { + userDao.delete(userId) + } + } diff --git a/codebrag-ui/app/scripts/users/manageUsersPopupCtrl.js b/codebrag-ui/app/scripts/users/manageUsersPopupCtrl.js index 747172dc..16d9491e 100644 --- a/codebrag-ui/app/scripts/users/manageUsersPopupCtrl.js +++ b/codebrag-ui/app/scripts/users/manageUsersPopupCtrl.js @@ -26,6 +26,15 @@ angular.module('codebrag.userMgmt') }); }; + $scope.deleteUser = function(userId) { + $scope.flash.clear(); + var userData = { userId: userId }; + userMgmtService.deleteUser(userId).then(deleteSuccess, deleteFailed('active', userId)) + userMgmtService.loadUsers().then(function(users) { + $scope.users = users; + }); + }; + $scope.askForNewPassword = function(user) { $scope.flash.clear(); var modal = popupsService.openSetUserPasswordPopup(user); @@ -33,10 +42,21 @@ angular.module('codebrag.userMgmt') $scope.flash.add('info', 'User password changed'); }); }; + function deleteSuccess() { + $scope.flash.add('error', 'User is removed'); + } function modifySuccess() { $scope.flash.add('info', 'User details changed'); } + function deleteFailed(flag, userId) { + return function(errorsMap) { + $scope.flash.add('error', 'Could not change user details'); + flattenErrorsMap(errorsMap).forEach(function(error) { + $scope.flash.add('error', error); + }); + } + } function modifyFailed(flag, user) { return function(errorsMap) { diff --git a/codebrag-ui/app/scripts/users/userMgmtService.js b/codebrag-ui/app/scripts/users/userMgmtService.js index 21465f08..9a6facb4 100644 --- a/codebrag-ui/app/scripts/users/userMgmtService.js +++ b/codebrag-ui/app/scripts/users/userMgmtService.js @@ -19,6 +19,11 @@ angular.module('codebrag.userMgmt') return $http.put(modifyUserUrl, userData).then(null, modifyUserFailed); }; + this.deleteUser = function(userId) { + var modifyUserUrl = [usersApiUrl, '/', userId].join(''); + return $http.delete(modifyUserUrl).then(null, modifyUserFailed); + }; + function modifyUserFailed(response) { return $q.reject(response.data); } @@ -34,4 +39,4 @@ angular.module('codebrag.userMgmt') $modal.open(config) } - }); \ No newline at end of file + }); diff --git a/codebrag-ui/app/views/popups/manageUsers.html b/codebrag-ui/app/views/popups/manageUsers.html index af2df3aa..47a296b8 100644 --- a/codebrag-ui/app/views/popups/manageUsers.html +++ b/codebrag-ui/app/views/popups/manageUsers.html @@ -21,10 +21,13 @@

Manage team members
Admin?
- +
     
+ +
  
+ @@ -32,6 +35,7 @@

Manage team members + @@ -45,4 +49,4 @@

Manage team members - \ No newline at end of file +