mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 20:50:19 +08:00
fix: Deprecate the id field in group (#1987)
This commit is contained in:
@ -24,11 +24,10 @@ import (
|
||||
|
||||
type Group struct {
|
||||
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
|
||||
Name string `xorm:"varchar(100) notnull pk unique" json:"name"`
|
||||
Name string `xorm:"varchar(100) notnull pk unique index" json:"name"`
|
||||
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
||||
UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"`
|
||||
|
||||
Id string `xorm:"varchar(100) not null index" json:"id"`
|
||||
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
||||
Manager string `xorm:"varchar(100)" json:"manager"`
|
||||
ContactEmail string `xorm:"varchar(100)" json:"contactEmail"`
|
||||
@ -95,12 +94,12 @@ func getGroup(owner string, name string) (*Group, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func getGroupById(id string) (*Group, error) {
|
||||
if id == "" {
|
||||
func getGroupByName(name string) (*Group, error) {
|
||||
if name == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
group := Group{Id: id}
|
||||
group := Group{Name: name}
|
||||
existed, err := adapter.Engine.Get(&group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -135,10 +134,6 @@ func UpdateGroup(id string, group *Group) (bool, error) {
|
||||
}
|
||||
|
||||
func AddGroup(group *Group) (bool, error) {
|
||||
if group.Id == "" {
|
||||
group.Id = util.GenerateId()
|
||||
}
|
||||
|
||||
affected, err := adapter.Engine.Insert(group)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@ -164,7 +159,7 @@ func DeleteGroup(group *Group) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if count, err := adapter.Engine.Where("parent_id = ?", group.Id).Count(&Group{}); err != nil {
|
||||
if count, err := adapter.Engine.Where("parent_id = ?", group.Name).Count(&Group{}); err != nil {
|
||||
return false, err
|
||||
} else if count > 0 {
|
||||
return false, errors.New("group has children group")
|
||||
@ -183,7 +178,7 @@ func DeleteGroup(group *Group) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if _, err := session.Delete(&UserGroupRelation{GroupId: group.Id}); err != nil {
|
||||
if _, err := session.Delete(&UserGroupRelation{GroupName: group.Name}); err != nil {
|
||||
session.Rollback()
|
||||
return false, err
|
||||
}
|
||||
@ -215,9 +210,8 @@ func ConvertToTreeData(groups []*Group, parentId string) []*Group {
|
||||
Key: group.Name,
|
||||
Type: group.Type,
|
||||
Owner: group.Owner,
|
||||
Id: group.Id,
|
||||
}
|
||||
children := ConvertToTreeData(groups, group.Id)
|
||||
children := ConvertToTreeData(groups, group.Name)
|
||||
if len(children) > 0 {
|
||||
node.Children = children
|
||||
}
|
||||
|
@ -76,7 +76,6 @@ type User struct {
|
||||
SignupApplication string `xorm:"varchar(100)" json:"signupApplication"`
|
||||
Hash string `xorm:"varchar(100)" json:"hash"`
|
||||
PreHash string `xorm:"varchar(100)" json:"preHash"`
|
||||
Groups []string `xorm:"varchar(1000)" json:"groups"`
|
||||
AccessKey string `xorm:"varchar(100)" json:"accessKey"`
|
||||
AccessSecret string `xorm:"varchar(100)" json:"accessSecret"`
|
||||
|
||||
@ -167,6 +166,7 @@ type User struct {
|
||||
|
||||
Roles []*Role `json:"roles"`
|
||||
Permissions []*Permission `json:"permissions"`
|
||||
Groups []string `xorm:"groups varchar(1000)" json:"groups"`
|
||||
|
||||
LastSigninWrongTime string `xorm:"varchar(100)" json:"lastSigninWrongTime"`
|
||||
SigninWrongTimes int `json:"signinWrongTimes"`
|
||||
@ -725,7 +725,7 @@ func DeleteUser(user *User) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
affected, err = deleteRelationByUser(user.Id)
|
||||
affected, err = DeleteRelationByUserId(user.Id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -10,15 +10,15 @@ import (
|
||||
)
|
||||
|
||||
type UserGroupRelation struct {
|
||||
UserId string `xorm:"varchar(100) notnull pk" json:"userId"`
|
||||
GroupId string `xorm:"varchar(100) notnull pk" json:"groupId"`
|
||||
UserId string `xorm:"varchar(100) notnull pk" json:"userId"`
|
||||
GroupName string `xorm:"varchar(100) notnull pk" json:"groupName"`
|
||||
|
||||
CreatedTime string `xorm:"created" json:"createdTime"`
|
||||
UpdatedTime string `xorm:"updated" json:"updatedTime"`
|
||||
}
|
||||
|
||||
func updateUserGroupRelation(session *xorm.Session, user *User) (int64, error) {
|
||||
physicalGroupCount, err := session.Where("type = ?", "Physical").In("id", user.Groups).Count(Group{})
|
||||
physicalGroupCount, err := session.In("name", user.Groups).Count(Group{Type: "Physical"})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@ -27,7 +27,7 @@ func updateUserGroupRelation(session *xorm.Session, user *User) (int64, error) {
|
||||
}
|
||||
|
||||
groups := []*Group{}
|
||||
err = session.In("id", user.Groups).Find(&groups)
|
||||
err = session.In("name", user.Groups).Find(&groups)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@ -42,7 +42,7 @@ func updateUserGroupRelation(session *xorm.Session, user *User) (int64, error) {
|
||||
|
||||
relations := []*UserGroupRelation{}
|
||||
for _, group := range groups {
|
||||
relations = append(relations, &UserGroupRelation{UserId: user.Id, GroupId: group.Id})
|
||||
relations = append(relations, &UserGroupRelation{UserId: user.Id, GroupName: group.Name})
|
||||
}
|
||||
if len(relations) == 0 {
|
||||
return 1, nil
|
||||
@ -76,35 +76,35 @@ func RemoveUserFromGroup(owner, name, groupId string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func deleteUserGroupRelation(session *xorm.Session, userId, groupId string) (int64, error) {
|
||||
affected, err := session.ID(core.PK{userId, groupId}).Delete(&UserGroupRelation{})
|
||||
func DeleteUserGroupRelation(userId, groupId string) (int64, error) {
|
||||
affected, err := adapter.Engine.ID(core.PK{userId, groupId}).Delete(&UserGroupRelation{})
|
||||
return affected, err
|
||||
}
|
||||
|
||||
func deleteRelationByUser(id string) (int64, error) {
|
||||
func DeleteRelationByUserId(id string) (int64, error) {
|
||||
affected, err := adapter.Engine.Delete(&UserGroupRelation{UserId: id})
|
||||
return affected, err
|
||||
}
|
||||
|
||||
func GetGroupUserCount(id string, field, value string) (int64, error) {
|
||||
group, err := GetGroup(id)
|
||||
func GetGroupUserCount(groupName string, field, value string) (int64, error) {
|
||||
group, err := getGroupByName(groupName)
|
||||
if group == nil || err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if field == "" && value == "" {
|
||||
return adapter.Engine.Count(UserGroupRelation{GroupId: group.Id})
|
||||
return adapter.Engine.Count(UserGroupRelation{GroupName: group.Name})
|
||||
} else {
|
||||
return adapter.Engine.Table("user").
|
||||
Join("INNER", []string{"user_group_relation", "r"}, "user.id = r.user_id").
|
||||
Where("r.group_id = ?", group.Id).
|
||||
Where("r.group_name = ?", group.Name).
|
||||
And(fmt.Sprintf("user.%s LIKE ?", util.CamelToSnakeCase(field)), "%"+value+"%").
|
||||
Count()
|
||||
}
|
||||
}
|
||||
|
||||
func GetPaginationGroupUsers(id string, offset, limit int, field, value, sortField, sortOrder string) ([]*User, error) {
|
||||
group, err := GetGroup(id)
|
||||
func GetPaginationGroupUsers(groupName string, offset, limit int, field, value, sortField, sortOrder string) ([]*User, error) {
|
||||
group, err := getGroupByName(groupName)
|
||||
if group == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -112,7 +112,7 @@ func GetPaginationGroupUsers(id string, offset, limit int, field, value, sortFie
|
||||
users := []*User{}
|
||||
session := adapter.Engine.Table("user").
|
||||
Join("INNER", []string{"user_group_relation", "r"}, "user.id = r.user_id").
|
||||
Where("r.group_id = ?", group.Id)
|
||||
Where("r.group_name = ?", group.Name)
|
||||
|
||||
if offset != -1 && limit != -1 {
|
||||
session.Limit(limit, offset)
|
||||
@ -139,16 +139,16 @@ func GetPaginationGroupUsers(id string, offset, limit int, field, value, sortFie
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func GetGroupUsers(id string) ([]*User, error) {
|
||||
group, err := GetGroup(id)
|
||||
func GetGroupUsers(groupName string) ([]*User, error) {
|
||||
group, err := getGroupByName(groupName)
|
||||
if group == nil || err != nil {
|
||||
return []*User{}, err
|
||||
}
|
||||
|
||||
users := []*User{}
|
||||
err = adapter.Engine.Table("user_group_relation").Join("INNER", []string{"user", "u"}, "user_group_relation.user_id = u.id").
|
||||
Where("user_group_relation.group_id = ?", group.Id).
|
||||
Find(&users)
|
||||
err = adapter.Engine.Table("user").
|
||||
Join("INNER", []string{"user_group_relation", "r"}, "user.id = r.user_id").
|
||||
Where("r.group_name = ?", group.Name).Find(&users)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user