This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Added delete a user service ,Added remove icon and refresh the user list #13
Open
gmkumar2005
wants to merge
5
commits into
softwaremill:master
Choose a base branch
from
gmkumar2005:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f71a024
Delete a user service changes
gmkumar2005 a6bd633
Added remove icon and refresh the user list after remove
gmkumar2005 75b9f3c
Added delete a user service ,Added remove icon and refresh the user list
gmkumar2005 272d73c
Merge remote-tracking branch 'origin'
gmkumar2005 4721493
Added DeleteUserUseCaseSpec and test cases. Enhanced error message on ui
gmkumar2005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this empty line |
||
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))) | ||
} | ||
} | ||
|
||
def delete(userId: ObjectId) { | ||
db.withTransaction { implicit session => | ||
users.filter(_.id === userId).delete | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,7 @@ trait UserDAO { | |
def countAll(): Long | ||
|
||
def countAllActive(): Long | ||
|
||
def delete(userId: ObjectId) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,7 +370,38 @@ class SQLUserDAOSpec extends FlatSpecWithSQL with ClearSQLDataAfterTest with Bef | |
foundUser.get.notifications.commits.get.getMillis should equal(commitDate.getMillis) | ||
foundUser.get.notifications.followups.get.getMillis should equal(followupDate.getMillis) | ||
} | ||
it should "deleta a user " taggedAs RequiresDb in { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo -> 'delete' |
||
// given | ||
val user = UserAssembler.randomUser.withBasicAuth("user", "pass").withAdmin(set = false).withActive(set = false).get | ||
userDAO.add(user) | ||
val newAuth = Authentication.basic(user.authentication.username, "newpass") | ||
|
||
// when | ||
val modifiedUser = user.copy(authentication = newAuth, admin = true, active = true) | ||
userDAO.delete(modifiedUser.id) | ||
val deletedUser = userDAO.findById(user.id) | ||
|
||
// then | ||
assert(deletedUser === None , "Deletion was attempted but found " + deletedUser) | ||
} | ||
it should "deleta a user and should not show in the list " taggedAs RequiresDb in { | ||
// given | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo -> 'delete' |
||
val user = UserAssembler.randomUser.withBasicAuth("user", "pass").withAdmin(set = false).withActive(set = false).get | ||
userDAO.add(user) | ||
val newAuth = Authentication.basic(user.authentication.username, "newpass") | ||
|
||
// when | ||
val tobeDeletedUser = user.copy(authentication = newAuth, admin = true, active = true) | ||
val userCountBeforeDelete = userDAO.findAll().length | ||
userDAO.delete(tobeDeletedUser.id) | ||
val deletedUser = userDAO.findById(user.id) | ||
val userCountAfterDelete = userDAO.findAll().length | ||
// then | ||
assert(deletedUser === None , "Deletion was attempted but found " + deletedUser) | ||
userCountAfterDelete should be(userCountBeforeDelete -1) | ||
|
||
} | ||
|
||
"rememberNotifications" should "store dates properly" taggedAs RequiresDb in { | ||
// given | ||
val user = UserAssembler.randomUser.get | ||
|
@@ -479,4 +510,5 @@ class SQLUserDAOSpec extends FlatSpecWithSQL with ClearSQLDataAfterTest with Bef | |
partial should have size (2) | ||
partial.map(_.name).toSet should be (users.map(_.name).toSet) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ag-service/src/main/scala/com/softwaremill/codebrag/usecases/user/DeleteUserUseCase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.softwaremill.codebrag.usecases.user | ||
|
||
import com.softwaremill.codebrag.dao.user.UserDAO | ||
import com.softwaremill.codebrag.usecases.assertions.UserAssertions | ||
import org.bson.types.ObjectId | ||
import com.softwaremill.codebrag.domain.{Authentication, User} | ||
import com.typesafe.scalalogging.slf4j.Logging | ||
import com.softwaremill.scalaval.Validation._ | ||
|
||
case class DeleteUserForm(userId: ObjectId) { | ||
|
||
} | ||
|
||
class DeleteUserUseCase(protected val userDao: UserDAO) extends Logging { | ||
|
||
import UserAssertions._ | ||
|
||
def execute(executorId: ObjectId, form: DeleteUserForm): Either[Errors, Unit] = { | ||
assertUserWithId(executorId, mustBeActive, mustBeAdmin)(userDao) | ||
val targetUser = loadUser(form.userId) | ||
validateUserDetails(executorId, targetUser, form).whenOk[Unit] { | ||
logger.debug(s"Validation passed, attempting to delete user $targetUser") | ||
userDao.delete(form.userId) | ||
} | ||
} | ||
|
||
private def loadUser(userId: ObjectId) = userDao.findById(userId).getOrElse(throw new IllegalStateException(s"User $userId not found")) | ||
|
||
private def validateUserDetails(executorId: ObjectId, user: User, form: DeleteUserForm) = { | ||
val changeOwnFlagsCheck = rule("userId") { | ||
val isDeleteFlags = user.admin || user.active | ||
(!isDeleteFlags || (isDeleteFlags && executorId != user.id), "Cannot delete own user") | ||
} | ||
validate(changeOwnFlagsCheck) | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
...ervice/src/test/scala/com/softwaremill/codebrag/usecases/user/DeleteUserUseCaseSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.softwaremill.codebrag.usecases.user | ||
|
||
import org.scalatest.{BeforeAndAfter, FlatSpec} | ||
import org.scalatest.matchers.ShouldMatchers | ||
import org.scalatest.mock.MockitoSugar | ||
import com.softwaremill.codebrag.dao.user.UserDAO | ||
import org.mockito.Matchers._ | ||
import org.mockito.Mockito._ | ||
import com.softwaremill.codebrag.domain.builder.UserAssembler | ||
import com.softwaremill.codebrag.domain.{Authentication, User} | ||
import com.softwaremill.codebrag.usecases.assertions.{ActiveUserStatusRequiredException, AdminRoleRequiredException} | ||
import org.bson.types.ObjectId | ||
|
||
class DeleteUserUseCaseSpec extends FlatSpec with ShouldMatchers with BeforeAndAfter with MockitoSugar { | ||
|
||
val userDao = mock[UserDAO] | ||
val useCase = new DeleteUserUseCase(userDao) | ||
|
||
val InactiveUser = UserAssembler.randomUser.withActive(set = false).get | ||
val ActiveUser = UserAssembler.randomUser.withActive().get | ||
|
||
val ValidExecutor = UserAssembler.randomUser.withAdmin().withActive().get | ||
val NonAdminExecutor = UserAssembler.randomUser.withActive().withAdmin(set = false).get | ||
val InactiveExecutor = UserAssembler.randomUser.withActive(set = false).withAdmin().get | ||
|
||
after { | ||
reset(userDao) | ||
} | ||
|
||
it should "not delete user when executing user is neither admin nor active" in { | ||
// given | ||
setupReturningUserFromDB(NonAdminExecutor, InactiveExecutor) | ||
val form = DeleteUserForm(ActiveUser.id) | ||
|
||
// when | ||
intercept[AdminRoleRequiredException] { | ||
useCase.execute(NonAdminExecutor.id, form) | ||
} | ||
intercept[ActiveUserStatusRequiredException] { | ||
useCase.execute(InactiveExecutor.id, form) | ||
} | ||
|
||
// then | ||
verify(userDao, never()).delete(any[ObjectId]) | ||
} | ||
|
||
it should "not allow to delete yourself" in { | ||
// given | ||
setupReturningUserFromDB(ValidExecutor) | ||
|
||
// when | ||
val ownChangeForm = DeleteUserForm(ValidExecutor.id) | ||
val Left(result) = useCase.execute(ValidExecutor.id, ownChangeForm) | ||
|
||
// then | ||
result should be(Map("userId" -> List("Cannot delete own user"))) | ||
verify(userDao, never()).delete(any[ObjectId]) | ||
} | ||
|
||
|
||
it should "delete user when validation passes" in { | ||
// given | ||
stubCurrentlyActiveUsersCountTo(0) | ||
setupReturningUserFromDB(ValidExecutor, ActiveUser) | ||
|
||
// when | ||
val newAuth = Authentication.basic(ActiveUser.authentication.username, "secret") | ||
val form = new DeleteUserForm(ActiveUser.id) | ||
val result = useCase.execute(ValidExecutor.id, form) | ||
|
||
// then | ||
result should be('right) | ||
val expectedUser = form | ||
verify(userDao).delete(expectedUser.userId) | ||
} | ||
|
||
private def stubCurrentlyActiveUsersCountTo(activeUsersCount: Int) { | ||
when(userDao.countAllActive()).thenReturn(activeUsersCount) | ||
} | ||
|
||
private def setupReturningUserFromDB(users: User*) { | ||
users.foreach { user => | ||
when(userDao.findById(user.id)).thenReturn(Some(user)) | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,17 +26,37 @@ angular.module('codebrag.userMgmt') | |
}); | ||
}; | ||
|
||
$scope.deleteUser = function(user) { | ||
$scope.flash.clear(); | ||
var userData = { userId: user.userId }; | ||
userMgmtService.deleteUser(userData).then(deleteSuccess(user), deleteFailed('active', user.userId)) | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please unident the whole section |
||
$scope.askForNewPassword = function(user) { | ||
$scope.flash.clear(); | ||
var modal = popupsService.openSetUserPasswordPopup(user); | ||
modal.result.then(function() { | ||
$scope.flash.add('info', 'User password changed'); | ||
}); | ||
}; | ||
function deleteSuccess(user) { | ||
$scope.flash.add('error', 'User ' + user.email + ' is deleted'); | ||
userMgmtService.loadUsers().then(function(users) { | ||
$scope.users = users; | ||
}); | ||
} | ||
|
||
function modifySuccess() { | ||
$scope.flash.add('info', 'User details changed'); | ||
} | ||
function deleteFailed(flag, userId) { | ||
return function(errorsMap) { | ||
$scope.flash.add('error', ' Could not delete user '); | ||
flattenErrorsMap(errorsMap).forEach(function(error) { | ||
$scope.flash.add('error', error); | ||
}); | ||
} | ||
} | ||
|
||
function modifyFailed(flag, user) { | ||
return function(errorsMap) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this empty line