2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
package models
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"time"
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
"xorm.io/xorm"
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
"code.vikunja.io/api/pkg/metrics"
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2019-07-16 16:15:40 +02:00
|
|
|
"code.vikunja.io/web"
|
2020-02-14 17:34:25 +01:00
|
|
|
"xorm.io/builder"
|
2019-07-16 16:15:40 +02:00
|
|
|
)
|
2018-12-01 00:26:56 +01:00
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
// Team holds a team object
|
|
|
|
type Team struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this team.
|
2020-12-18 17:51:22 +01:00
|
|
|
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"team"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The name of this team.
|
2020-09-26 23:02:17 +02:00
|
|
|
Name string `xorm:"varchar(250) not null" json:"name" valid:"required,runelength(1|250)" minLength:"1" maxLength:"250"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The team's description.
|
2019-07-21 23:57:19 +02:00
|
|
|
Description string `xorm:"longtext null" json:"description"`
|
2020-12-18 17:51:22 +01:00
|
|
|
CreatedByID int64 `xorm:"bigint not null INDEX" json:"-"`
|
2018-07-14 17:34:59 +02:00
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// The user who created this team.
|
2020-04-12 22:48:46 +02:00
|
|
|
CreatedBy *user.User `xorm:"-" json:"created_by"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// An array of all members in this team.
|
|
|
|
Members []*TeamUser `xorm:"-" json:"members"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this relation was created. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Created time.Time `xorm:"created" json:"created"`
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this relation was last updated. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Updated time.Time `xorm:"updated" json:"updated"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName makes beautiful table names
|
|
|
|
func (Team) TableName() string {
|
|
|
|
return "teams"
|
|
|
|
}
|
|
|
|
|
|
|
|
// TeamMember defines the relationship between a user and a team
|
|
|
|
type TeamMember struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this team member relation.
|
2020-12-18 17:51:22 +01:00
|
|
|
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The team id.
|
2020-12-18 17:51:22 +01:00
|
|
|
TeamID int64 `xorm:"bigint not null INDEX" json:"-" param:"team"`
|
2019-05-25 11:47:16 +02:00
|
|
|
// The username of the member. We use this to prevent automated user id entering.
|
|
|
|
Username string `xorm:"-" json:"username" param:"user"`
|
|
|
|
// Used under the hood to manage team members
|
2020-12-18 17:51:22 +01:00
|
|
|
UserID int64 `xorm:"bigint not null INDEX" json:"-"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// Whether or not the member is an admin of the team. See the docs for more about what a team admin can do
|
2020-02-16 22:42:04 +01:00
|
|
|
Admin bool `xorm:"null" json:"admin"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this relation was created. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Created time.Time `xorm:"created not null" json:"created"`
|
2018-07-14 18:29:24 +02:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName makes beautiful table names
|
|
|
|
func (TeamMember) TableName() string {
|
|
|
|
return "team_members"
|
|
|
|
}
|
|
|
|
|
2018-07-16 08:45:38 +02:00
|
|
|
// TeamUser is the team member type
|
2018-07-16 08:43:47 +02:00
|
|
|
type TeamUser struct {
|
2020-01-26 18:08:06 +01:00
|
|
|
user.User `xorm:"extends"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// Whether or not the member is an admin of the team. See the docs for more about what a team admin can do
|
2020-01-27 18:28:17 +01:00
|
|
|
Admin bool `json:"admin"`
|
|
|
|
TeamID int64 `json:"-"`
|
2018-07-16 08:43:47 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 18:29:24 +02:00
|
|
|
// GetTeamByID gets a team by its ID
|
2020-12-23 16:32:28 +01:00
|
|
|
func GetTeamByID(s *xorm.Session, id int64) (team *Team, err error) {
|
2018-09-13 20:07:11 +02:00
|
|
|
if id < 1 {
|
|
|
|
return team, ErrTeamDoesNotExist{id}
|
|
|
|
}
|
|
|
|
|
2020-01-27 18:28:17 +01:00
|
|
|
t := Team{}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
exists, err := s.
|
2020-01-27 18:28:17 +01:00
|
|
|
Where("id = ?", id).
|
|
|
|
Get(&t)
|
2018-07-14 18:29:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !exists {
|
2020-01-27 18:28:17 +01:00
|
|
|
return &t, ErrTeamDoesNotExist{id}
|
2018-07-14 18:29:24 +02:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:28:17 +01:00
|
|
|
teamSlice := []*Team{&t}
|
2020-12-23 16:32:28 +01:00
|
|
|
err = addMoreInfoToTeams(s, teamSlice)
|
2020-01-27 18:28:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
team = &t
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
func addMoreInfoToTeams(s *xorm.Session, teams []*Team) (err error) {
|
2020-01-27 18:28:17 +01:00
|
|
|
// Put the teams in a map to make assigning more info to it more efficient
|
|
|
|
teamMap := make(map[int64]*Team, len(teams))
|
|
|
|
var teamIDs []int64
|
|
|
|
var ownerIDs []int64
|
|
|
|
for _, team := range teams {
|
|
|
|
teamMap[team.ID] = team
|
|
|
|
teamIDs = append(teamIDs, team.ID)
|
|
|
|
ownerIDs = append(ownerIDs, team.CreatedByID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all owners and team members
|
2020-02-16 22:42:04 +01:00
|
|
|
users := make(map[int64]*TeamUser)
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.
|
|
|
|
Select("*").
|
2020-01-27 18:28:17 +01:00
|
|
|
Table("users").
|
|
|
|
Join("LEFT", "team_members", "team_members.user_id = users.id").
|
|
|
|
Join("LEFT", "teams", "team_members.team_id = teams.id").
|
|
|
|
Or(
|
|
|
|
builder.In("team_id", teamIDs),
|
|
|
|
builder.And(
|
|
|
|
builder.In("users.id", ownerIDs),
|
|
|
|
builder.Expr("teams.created_by_id = users.id"),
|
|
|
|
builder.In("teams.id", teamIDs),
|
|
|
|
),
|
|
|
|
).
|
|
|
|
Find(&users)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, u := range users {
|
|
|
|
if _, exists := teamMap[u.TeamID]; !exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
u.Email = ""
|
|
|
|
teamMap[u.TeamID].Members = append(teamMap[u.TeamID].Members, u)
|
|
|
|
}
|
2020-02-16 22:42:04 +01:00
|
|
|
|
|
|
|
// We need to do this in a second loop as owners might not be the last ones in the list
|
|
|
|
for _, team := range teamMap {
|
|
|
|
if teamUser, has := users[team.CreatedByID]; has {
|
|
|
|
team.CreatedBy = &teamUser.User
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 18:29:24 +02:00
|
|
|
return
|
|
|
|
}
|
2018-07-16 08:43:47 +02:00
|
|
|
|
|
|
|
// ReadOne implements the CRUD method to get one team
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Gets one team
|
|
|
|
// @Description Returns a team by its ID.
|
|
|
|
// @tags team
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Param id path int true "Team ID"
|
|
|
|
// @Success 200 {object} models.Team "The team"
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 403 {object} web.HTTPError "The user does not have access to the team"
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
2020-01-27 18:28:17 +01:00
|
|
|
// @Router /teams/{id} [get]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (t *Team) ReadOne(s *xorm.Session) (err error) {
|
|
|
|
team, err := GetTeamByID(s, t.ID)
|
2020-01-27 18:28:17 +01:00
|
|
|
if team != nil {
|
|
|
|
*t = *team
|
|
|
|
}
|
2018-07-16 08:43:47 +02:00
|
|
|
return
|
|
|
|
}
|
2018-07-16 08:52:16 +02:00
|
|
|
|
|
|
|
// ReadAll gets all teams the user is part of
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Get teams
|
|
|
|
// @Description Returns all teams the current user is part of.
|
|
|
|
// @tags team
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-10-23 23:11:40 +02:00
|
|
|
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
|
|
|
|
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Param s query string false "Search teams by its name."
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Success 200 {array} models.Team "The teams."
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /teams [get]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (t *Team) ReadAll(s *xorm.Session, a web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
|
2019-08-31 22:56:41 +02:00
|
|
|
if _, is := a.(*LinkSharing); is {
|
2019-10-23 23:11:40 +02:00
|
|
|
return nil, 0, 0, ErrGenericForbidden{}
|
2019-08-31 22:56:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:29:24 +02:00
|
|
|
limit, start := getLimitFromPageIndex(page, perPage)
|
|
|
|
|
2018-07-16 08:52:16 +02:00
|
|
|
all := []*Team{}
|
2020-12-23 16:32:28 +01:00
|
|
|
query := s.Select("teams.*").
|
2018-07-16 08:52:16 +02:00
|
|
|
Table("teams").
|
|
|
|
Join("INNER", "team_members", "team_members.team_id = teams.id").
|
2019-06-28 10:21:48 +02:00
|
|
|
Where("team_members.user_id = ?", a.GetID()).
|
2020-04-12 19:29:24 +02:00
|
|
|
Where("teams.name LIKE ?", "%"+search+"%")
|
|
|
|
if limit > 0 {
|
|
|
|
query = query.Limit(limit, start)
|
|
|
|
}
|
|
|
|
err = query.Find(&all)
|
2019-10-23 23:11:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
2018-07-16 08:52:16 +02:00
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
err = addMoreInfoToTeams(s, all)
|
2020-01-27 18:28:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
numberOfTotalItems, err = s.
|
2019-10-23 23:11:40 +02:00
|
|
|
Table("teams").
|
|
|
|
Join("INNER", "team_members", "team_members.team_id = teams.id").
|
|
|
|
Where("team_members.user_id = ?", a.GetID()).
|
|
|
|
Where("teams.name LIKE ?", "%"+search+"%").
|
|
|
|
Count(&Team{})
|
|
|
|
return all, len(all), numberOfTotalItems, err
|
2018-07-16 08:52:16 +02:00
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
// Create is the handler to create a team
|
|
|
|
// @Summary Creates a new team
|
|
|
|
// @Description Creates a new team in a given namespace. The user needs write-access to the namespace.
|
|
|
|
// @tags team
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param team body models.Team true "The team you want to create."
|
|
|
|
// @Success 200 {object} models.Team "The created team."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid team object provided."
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /teams [put]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (t *Team) Create(s *xorm.Session, a web.Auth) (err error) {
|
2020-01-26 18:08:06 +01:00
|
|
|
doer, err := user.GetFromAuth(a)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have a name
|
|
|
|
if t.Name == "" {
|
|
|
|
return ErrTeamNameCannotBeEmpty{}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.CreatedByID = doer.ID
|
2019-08-14 21:59:31 +02:00
|
|
|
t.CreatedBy = doer
|
2019-07-16 16:15:40 +02:00
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.Insert(t)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the current user as member and admin
|
|
|
|
tm := TeamMember{TeamID: t.ID, Username: doer.Username, Admin: true}
|
2020-12-23 16:32:28 +01:00
|
|
|
if err = tm.Create(s, doer); err != nil {
|
2019-07-16 16:15:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.UpdateCount(1, metrics.TeamCountKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a team
|
|
|
|
// @Summary Deletes a team
|
|
|
|
// @Description Delets a team. This will also remove the access for all users in that team.
|
|
|
|
// @tags team
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "Team ID"
|
|
|
|
// @Success 200 {object} models.Message "The team was successfully deleted."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid team object provided."
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /teams/{id} [delete]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (t *Team) Delete(s *xorm.Session) (err error) {
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
// Delete the team
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.ID(t.ID).Delete(&Team{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete team members
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.Where("team_id = ?", t.ID).Delete(&TeamMember{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete team <-> namespace relations
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.Where("team_id = ?", t.ID).Delete(&TeamNamespace{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete team <-> lists relations
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.Where("team_id = ?", t.ID).Delete(&TeamList{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.UpdateCount(-1, metrics.TeamCountKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is the handler to create a team
|
|
|
|
// @Summary Updates a team
|
|
|
|
// @Description Updates a team.
|
|
|
|
// @tags team
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "Team ID"
|
|
|
|
// @Param team body models.Team true "The team with updated values you want to update."
|
|
|
|
// @Success 200 {object} models.Team "The updated team."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid team object provided."
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /teams/{id} [post]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (t *Team) Update(s *xorm.Session) (err error) {
|
2019-07-16 16:15:40 +02:00
|
|
|
// Check if we have a name
|
|
|
|
if t.Name == "" {
|
|
|
|
return ErrTeamNameCannotBeEmpty{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the team exists
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = GetTeamByID(s, t.ID)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.ID(t.ID).Update(t)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the newly updated team
|
2020-12-23 16:32:28 +01:00
|
|
|
team, err := GetTeamByID(s, t.ID)
|
2020-01-27 18:28:17 +01:00
|
|
|
if team != nil {
|
|
|
|
*t = *team
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|