mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
feat: fix potential bugs in init_data.go (#2932)
* fix: fix potential bugs in init data * fix: improve code format
This commit is contained in:
parent
572616d390
commit
d24ddd4f1c
@ -154,6 +154,15 @@ func AddGroups(groups []*Group) (bool, error) {
|
||||
return affected != 0, nil
|
||||
}
|
||||
|
||||
func deleteGroup(group *Group) (bool, error) {
|
||||
affected, err := ormer.Engine.ID(core.PK{group.Owner, group.Name}).Delete(&Group{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return affected != 0, nil
|
||||
}
|
||||
|
||||
func DeleteGroup(group *Group) (bool, error) {
|
||||
_, err := ormer.Engine.Get(group)
|
||||
if err != nil {
|
||||
@ -172,12 +181,7 @@ func DeleteGroup(group *Group) (bool, error) {
|
||||
return false, errors.New("group has users")
|
||||
}
|
||||
|
||||
affected, err := ormer.Engine.ID(core.PK{group.Owner, group.Name}).Delete(&Group{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return affected != 0, nil
|
||||
return deleteGroup(group)
|
||||
}
|
||||
|
||||
func checkGroupName(name string) error {
|
||||
|
@ -311,7 +311,7 @@ func initDefinedUser(user *User) {
|
||||
panic(err)
|
||||
}
|
||||
if existed != nil {
|
||||
affected, err := DeleteUser(user)
|
||||
affected, err := deleteUser(user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -423,7 +423,7 @@ func initDefinedPermission(permission *Permission) {
|
||||
}
|
||||
|
||||
if existed != nil {
|
||||
affected, err := DeletePermission(permission)
|
||||
affected, err := deletePermission(permission)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -511,7 +511,7 @@ func initDefinedRole(role *Role) {
|
||||
}
|
||||
|
||||
if existed != nil {
|
||||
affected, err := DeleteRole(role)
|
||||
affected, err := deleteRole(role)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -598,7 +598,7 @@ func initDefinedGroup(group *Group) {
|
||||
panic(err)
|
||||
}
|
||||
if existed != nil {
|
||||
affected, err := DeleteGroup(group)
|
||||
affected, err := deleteGroup(group)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -286,13 +286,22 @@ func AddPermissionsInBatch(permissions []*Permission) (bool, error) {
|
||||
return affected, nil
|
||||
}
|
||||
|
||||
func DeletePermission(permission *Permission) (bool, error) {
|
||||
func deletePermission(permission *Permission) (bool, error) {
|
||||
affected, err := ormer.Engine.ID(core.PK{permission.Owner, permission.Name}).Delete(&Permission{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if affected != 0 {
|
||||
return affected != 0, nil
|
||||
}
|
||||
|
||||
func DeletePermission(permission *Permission) (bool, error) {
|
||||
affected, err := deletePermission(permission)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if affected {
|
||||
err = removeGroupingPolicies(permission)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@ -314,7 +323,7 @@ func DeletePermission(permission *Permission) (bool, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return affected != 0, nil
|
||||
return affected, nil
|
||||
}
|
||||
|
||||
func getPermissionsByUser(userId string) ([]*Permission, error) {
|
||||
|
@ -238,6 +238,15 @@ func AddRolesInBatch(roles []*Role) bool {
|
||||
return affected
|
||||
}
|
||||
|
||||
func deleteRole(role *Role) (bool, error) {
|
||||
affected, err := ormer.Engine.ID(core.PK{role.Owner, role.Name}).Delete(&Role{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return affected != 0, nil
|
||||
}
|
||||
|
||||
func DeleteRole(role *Role) (bool, error) {
|
||||
roleId := role.GetId()
|
||||
permissions, err := GetPermissionsByRole(roleId)
|
||||
@ -253,12 +262,7 @@ func DeleteRole(role *Role) (bool, error) {
|
||||
}
|
||||
}
|
||||
|
||||
affected, err := ormer.Engine.ID(core.PK{role.Owner, role.Name}).Delete(&Role{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return affected != 0, nil
|
||||
return deleteRole(role)
|
||||
}
|
||||
|
||||
func (role *Role) GetId() string {
|
||||
|
@ -919,6 +919,15 @@ func AddUsersInBatch(users []*User) (bool, error) {
|
||||
return affected, nil
|
||||
}
|
||||
|
||||
func deleteUser(user *User) (bool, error) {
|
||||
affected, err := ormer.Engine.ID(core.PK{user.Owner, user.Name}).Delete(&User{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return affected != 0, nil
|
||||
}
|
||||
|
||||
func DeleteUser(user *User) (bool, error) {
|
||||
// Forced offline the user first
|
||||
_, err := DeleteSession(util.GetSessionId(user.Owner, user.Name, CasdoorApplication))
|
||||
@ -926,12 +935,7 @@ func DeleteUser(user *User) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
affected, err := ormer.Engine.ID(core.PK{user.Owner, user.Name}).Delete(&User{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return affected != 0, nil
|
||||
return deleteUser(user)
|
||||
}
|
||||
|
||||
func GetUserInfo(user *User, scope string, aud string, host string) (*Userinfo, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user