Commit 9e7aa65f by Daniel Lee

WIP: clean up after user and org user delete

parent 699f9095
...@@ -154,6 +154,56 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -154,6 +154,56 @@ func TestAccountDataAccess(t *testing.T) {
So(err, ShouldEqual, m.ErrLastOrgAdmin) So(err, ShouldEqual, m.ErrLastOrgAdmin)
}) })
Convey("Given an org user with dashboard permissions", func() {
ac3cmd := m.CreateUserCommand{Login: "ac3", Email: "ac3@test.com", Name: "ac3 name", IsAdmin: false}
err := CreateUser(&ac3cmd)
So(err, ShouldBeNil)
ac3 := ac3cmd.Result
orgUserCmd := m.AddOrgUserCommand{
OrgId: ac1.OrgId,
UserId: ac3.Id,
Role: m.ROLE_VIEWER,
}
err = AddOrgUser(&orgUserCmd)
So(err, ShouldBeNil)
query := m.GetOrgUsersQuery{OrgId: orgUserCmd.OrgId}
err = GetOrgUsers(&query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 3)
err = AddOrUpdateDashboardPermission(&m.AddOrUpdateDashboardPermissionCommand{DashboardId: 1, OrgId: ac1.OrgId, UserId: ac3.Id, PermissionType: m.PERMISSION_EDIT})
So(err, ShouldBeNil)
err = AddOrUpdateDashboardPermission(&m.AddOrUpdateDashboardPermissionCommand{DashboardId: 2, OrgId: ac3.OrgId, UserId: ac3.Id, PermissionType: m.PERMISSION_EDIT})
So(err, ShouldBeNil)
Convey("When org user is deleted", func() {
cmdRemove := m.RemoveOrgUserCommand{OrgId: orgUserCmd.OrgId, UserId: ac3.Id}
err := RemoveOrgUser(&cmdRemove)
So(err, ShouldBeNil)
Convey("Should remove dependent permissions for deleted org user", func() {
permQuery := &m.GetDashboardPermissionsQuery{DashboardId: 1}
err = GetDashboardPermissions(permQuery)
So(err, ShouldBeNil)
So(len(permQuery.Result), ShouldEqual, 0)
})
Convey("Should not remove dashboard permissions for same user in another org", func() {
permQuery := &m.GetDashboardPermissionsQuery{DashboardId: 2}
err = GetDashboardPermissions(permQuery)
So(err, ShouldBeNil)
So(permQuery.Result[0].OrgId, ShouldEqual, ac3.OrgId)
So(permQuery.Result[0].UserId, ShouldEqual, ac3.Id)
})
})
})
}) })
}) })
}) })
......
...@@ -80,10 +80,17 @@ func GetOrgUsers(query *m.GetOrgUsersQuery) error { ...@@ -80,10 +80,17 @@ func GetOrgUsers(query *m.GetOrgUsersQuery) error {
func RemoveOrgUser(cmd *m.RemoveOrgUserCommand) error { func RemoveOrgUser(cmd *m.RemoveOrgUserCommand) error {
return inTransaction(func(sess *DBSession) error { return inTransaction(func(sess *DBSession) error {
var rawSql = "DELETE FROM org_user WHERE org_id=? and user_id=?" deletes := []string{
_, err := sess.Exec(rawSql, cmd.OrgId, cmd.UserId) "DELETE FROM org_user WHERE org_id=? and user_id=?",
if err != nil { "DELETE FROM dashboard_acl WHERE org_id=? and user_id = ?",
return err "DELETE FROM user_group_member WHERE org_id=? and user_id = ?",
}
for _, sql := range deletes {
_, err := sess.Exec(sql, cmd.OrgId, cmd.UserId)
if err != nil {
return err
}
} }
return validateOneAdminLeftInOrg(cmd.OrgId, sess) return validateOneAdminLeftInOrg(cmd.OrgId, sess)
......
...@@ -399,6 +399,7 @@ func DeleteUser(cmd *m.DeleteUserCommand) error { ...@@ -399,6 +399,7 @@ func DeleteUser(cmd *m.DeleteUserCommand) error {
"DELETE FROM org_user WHERE user_id = ?", "DELETE FROM org_user WHERE user_id = ?",
"DELETE FROM dashboard_acl WHERE user_id = ?", "DELETE FROM dashboard_acl WHERE user_id = ?",
"DELETE FROM preferences WHERE user_id = ?", "DELETE FROM preferences WHERE user_id = ?",
"DELETE FROM user_group_member WHERE user_id = ?",
} }
for _, sql := range deletes { for _, sql := range deletes {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment