diff --git a/controllers/user.go b/controllers/user.go index ce589a45..fa4a88dd 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -80,7 +80,7 @@ func (c *ApiController) GetGlobalUsers() { // @router /get-users [get] func (c *ApiController) GetUsers() { owner := c.Input().Get("owner") - groupId := c.Input().Get("groupId") + groupName := c.Input().Get("groupName") limit := c.Input().Get("pageSize") page := c.Input().Get("p") field := c.Input().Get("field") @@ -89,8 +89,8 @@ func (c *ApiController) GetUsers() { sortOrder := c.Input().Get("sortOrder") if limit == "" || page == "" { - if groupId != "" { - maskedUsers, err := object.GetMaskedUsers(object.GetGroupUsers(groupId)) + if groupName != "" { + maskedUsers, err := object.GetMaskedUsers(object.GetGroupUsers(groupName)) if err != nil { c.ResponseError(err.Error()) return @@ -108,14 +108,14 @@ func (c *ApiController) GetUsers() { c.ServeJSON() } else { limit := util.ParseInt(limit) - count, err := object.GetUserCount(owner, field, value, groupId) + count, err := object.GetUserCount(owner, field, value, groupName) if err != nil { c.ResponseError(err.Error()) return } paginator := pagination.SetPaginator(c.Ctx, limit, count) - users, err := object.GetPaginationUsers(owner, paginator.Offset(), limit, field, value, sortField, sortOrder, groupId) + users, err := object.GetPaginationUsers(owner, paginator.Offset(), limit, field, value, sortField, sortOrder, groupName) if err != nil { c.ResponseError(err.Error()) return diff --git a/object/group.go b/object/group.go index 2ea08a53..6fbaa733 100644 --- a/object/group.go +++ b/object/group.go @@ -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 } diff --git a/object/user.go b/object/user.go index 39de7d33..6be1d108 100644 --- a/object/user.go +++ b/object/user.go @@ -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 } diff --git a/object/user_group.go b/object/user_group.go index 8a61c994..24d8a83a 100644 --- a/object/user_group.go +++ b/object/user_group.go @@ -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 } diff --git a/web/src/GroupEdit.js b/web/src/GroupEdit.js index 36566121..02b9ff54 100644 --- a/web/src/GroupEdit.js +++ b/web/src/GroupEdit.js @@ -44,11 +44,6 @@ class GroupEditPage extends React.Component { GroupBackend.getGroup(this.state.organizationName, this.state.groupName) .then((res) => { if (res.status === "ok") { - if (res.data === null) { - this.props.history.push("/404"); - return; - } - this.setState({ group: res.data, }); @@ -96,12 +91,12 @@ class GroupEditPage extends React.Component { } getParentIdOptions() { - const groups = this.state.groups.filter((group) => group.id !== this.state.group.id); + const groups = this.state.groups.filter((group) => group.name !== this.state.group.name); const organization = this.state.organizations.find((organization) => organization.name === this.state.group.owner); if (organization !== undefined) { - groups.push({id: organization.name, displayName: organization.displayName}); + groups.push({name: organization.name, displayName: organization.displayName}); } - return groups.map((group) => ({label: group.displayName, value: group.id})); + return groups.map((group) => ({label: group.displayName, value: group.name})); } renderGroup() { @@ -136,7 +131,7 @@ class GroupEditPage extends React.Component { {Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} : - { + { this.updateGroupField("name", e.target.value); }} /> diff --git a/web/src/GroupList.js b/web/src/GroupList.js index 6a31c5eb..32b13a4a 100644 --- a/web/src/GroupList.js +++ b/web/src/GroupList.js @@ -185,7 +185,7 @@ class GroupListPage extends BaseListPage { {record.parentId} ; } - const parentGroup = this.state.groups.find((group) => group.id === text); + const parentGroup = this.state.groups.find((group) => group.name === text); if (parentGroup === undefined) { return ""; } diff --git a/web/src/GroupTreePage.js b/web/src/GroupTreePage.js index 912211a5..c4cf76e5 100644 --- a/web/src/GroupTreePage.js +++ b/web/src/GroupTreePage.js @@ -30,7 +30,6 @@ class GroupTreePage extends React.Component { owner: Setting.isAdminUser(this.props.account) ? "" : this.props.account.owner, organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName, groupName: this.props.match?.params.groupName, - groupId: undefined, treeData: [], selectedKeys: [this.props.match?.params.groupName], }; @@ -55,7 +54,6 @@ class GroupTreePage extends React.Component { if (res.status === "ok") { this.setState({ treeData: res.data, - groupId: this.findNodeId({children: res.data}, this.state.groupName), }); } else { Setting.showMessage("error", res.msg); @@ -63,26 +61,10 @@ class GroupTreePage extends React.Component { }); } - findNodeId(node, targetName) { - if (node.key === targetName) { - return node.id; - } - if (node.children) { - for (let i = 0; i < node.children.length; i++) { - const result = this.findNodeId(node.children[i], targetName); - if (result) { - return result; - } - } - } - return null; - } - setTreeTitle(treeData) { const haveChildren = Array.isArray(treeData.children) && treeData.children.length > 0; const isSelected = this.state.groupName === treeData.key; return { - id: treeData.id, key: treeData.key, title: {treeData.type === "Physical" ? : } @@ -201,7 +183,6 @@ class GroupTreePage extends React.Component { this.setState({ selectedKeys: selectedKeys, groupName: info.node.key, - groupId: info.node.id, }); this.props.history.push(`/trees/${this.state.organizationName}/${info.node.key}`); }; @@ -257,7 +238,7 @@ class GroupTreePage extends React.Component { updatedTime: moment().format(), displayName: `New Group - ${randomName}`, type: "Virtual", - parentId: isRoot ? this.state.organizationName : this.state.groupId, + parentId: isRoot ? this.state.organizationName : this.state.groupName, isTopGroup: isRoot, isEnabled: true, }; @@ -300,8 +281,7 @@ class GroupTreePage extends React.Component { onClick={() => { this.setState({ selectedKeys: [], - groupName: null, - groupId: undefined, + groupName: undefined, }); this.props.history.push(`/trees/${this.state.organizationName}`); }} @@ -323,7 +303,6 @@ class GroupTreePage extends React.Component { diff --git a/web/src/UserEditPage.js b/web/src/UserEditPage.js index 8e538a18..754251a6 100644 --- a/web/src/UserEditPage.js +++ b/web/src/UserEditPage.js @@ -309,7 +309,7 @@ class UserEditPage extends React.Component {