Added method to add a user to a team
This commit is contained in:
parent
0b2f66965a
commit
bc580e0115
8 changed files with 66 additions and 8 deletions
|
@ -8,3 +8,5 @@ Content-Type: application/json
|
|||
}
|
||||
|
||||
> {% client.global.set("auth_token", response.body.token); %}
|
||||
|
||||
###
|
||||
|
|
29
REST-Tests/teams.http
Normal file
29
REST-Tests/teams.http
Normal file
|
@ -0,0 +1,29 @@
|
|||
# Get all teams
|
||||
GET http://localhost:8080/api/v1/teams
|
||||
Authorization: Bearer {{auth_token}}
|
||||
|
||||
###
|
||||
|
||||
# Get one team
|
||||
GET http://localhost:8080/api/v1/teams/28
|
||||
Authorization: Bearer {{auth_token}}
|
||||
|
||||
###
|
||||
|
||||
# Add a new member to that team
|
||||
PUT http://localhost:8080/api/v1/teams/28/members
|
||||
Authorization: Bearer {{auth_token}}
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"user_id": 2
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
# Delete a member from a team
|
||||
DELETE http://localhost:8080/api/v1/teams/28/members/2
|
||||
Authorization: Bearer {{auth_token}}
|
||||
|
||||
###
|
||||
|
|
@ -2,7 +2,19 @@ package models
|
|||
|
||||
// Create implements the create method to assign a user to a team
|
||||
func (tm *TeamMember) Create(doer *User) (err error) {
|
||||
// TODO: Check if it exists etc
|
||||
// Check if the team extst
|
||||
_, err = GetTeamByID(tm.TeamID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the user exists
|
||||
_, _, err = GetUserByID(tm.UserID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Insert the user
|
||||
_, err = x.Insert(tm)
|
||||
return
|
||||
}
|
||||
|
|
9
models/team_members_rights.go
Normal file
9
models/team_members_rights.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package models
|
||||
|
||||
func (tm *TeamMember) CanCreate(user *User) bool {
|
||||
|
||||
// A user can add a member to a team if he is admin of that team
|
||||
exists, _ := x.Where("user_id = ? AND team_id = ? AND admin = ?", user.ID, tm.TeamID, true).
|
||||
Get(&TeamMember{})
|
||||
return exists
|
||||
}
|
|
@ -38,9 +38,9 @@ func (t *Team) AfterLoad() {
|
|||
// TeamMember defines the relationship between a user and a team
|
||||
type TeamMember struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
TeamID int64 `xorm:"int(11) not null" json:"team_id"`
|
||||
UserID int64 `xorm:"int(11) not null" json:"user_id"`
|
||||
IsAdmin bool `xorm:"tinyint(1)" json:"is_admin"`
|
||||
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"team"`
|
||||
UserID int64 `xorm:"int(11) not null" json:"user_id" param:"user"`
|
||||
Admin bool `xorm:"tinyint(1)" json:"admin"`
|
||||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
@ -57,7 +57,7 @@ func (TeamMember) TableName() string {
|
|||
// TeamUser is the team member type
|
||||
type TeamUser struct {
|
||||
User `xorm:"extends"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
Admin bool `json:"admin"`
|
||||
}
|
||||
|
||||
// GetAllTeamsByNamespaceID returns all teams for a namespace
|
||||
|
|
|
@ -16,7 +16,7 @@ func (t *Team) Create(doer *User) (err error) {
|
|||
}
|
||||
|
||||
// Insert the current user as member and admin
|
||||
tm := TeamMember{TeamID: t.ID, UserID: doer.ID, IsAdmin: true}
|
||||
tm := TeamMember{TeamID: t.ID, UserID: doer.ID, Admin: true}
|
||||
err = tm.Create(doer)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func (t *Team) CanUpdate(user *User) bool {
|
|||
// Check if the current user is in the team and has admin rights in it
|
||||
exists, _ := x.Where("team_id = ?", t.ID).
|
||||
And("user_id = ?", user.ID).
|
||||
And("is_admin = ?", true).
|
||||
And("admin = ?", true).
|
||||
Get(&TeamMember{})
|
||||
|
||||
return exists
|
||||
|
@ -54,7 +54,7 @@ func (t *Team) CanDelete(user *User) bool {
|
|||
func (t *Team) IsAdmin(user *User) bool {
|
||||
exists, _ := x.Where("team_id = ?", t.ID).
|
||||
And("user_id = ?", user.ID).
|
||||
And("is_admin = ?", true).
|
||||
And("admin = ?", true).
|
||||
Get(&TeamMember{})
|
||||
return exists
|
||||
}
|
||||
|
|
|
@ -157,4 +157,10 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.PUT("/teams", teamHandler.CreateWeb)
|
||||
a.POST("/teams/:team", teamHandler.UpdateWeb)
|
||||
a.DELETE("/teams/:team", teamHandler.DeleteWeb)
|
||||
|
||||
teamMemberHandler := &crud.WebHandler{
|
||||
CObject: &models.TeamMember{},
|
||||
}
|
||||
a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb)
|
||||
a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue