From 6627c074bd635c72544e6ca1d5425621a615bd50 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 25 May 2019 10:26:32 +0200 Subject: [PATCH] Added uid to teams --- pkg/migration/20190525101422.go | 71 +++++++++++++++++++++++++++++++++ pkg/models/teams.go | 2 + pkg/models/teams_create.go | 2 + 3 files changed, 75 insertions(+) create mode 100644 pkg/migration/20190525101422.go diff --git a/pkg/migration/20190525101422.go b/pkg/migration/20190525101422.go new file mode 100644 index 00000000000..45710da1ebf --- /dev/null +++ b/pkg/migration/20190525101422.go @@ -0,0 +1,71 @@ +// Vikunja is a todo-list application to facilitate your life. +// Copyright 2019 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// 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 +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package migration + +import ( + "code.vikunja.io/api/pkg/utils" + "github.com/go-xorm/xorm" + "src.techknowlogick.com/xormigrate" +) + +type team20190525101422 struct { + ID int64 `xorm:"int(11) autoincr not null unique pk"` + UID string `xorm:"varchar(32) not null default ''"` + Name string `xorm:"varchar(250) not null"` + Description string `xorm:"varchar(250)"` + CreatedByID int64 `xorm:"int(11) not null INDEX"` + Created int64 `xorm:"created"` + Updated int64 `xorm:"updated"` +} + +// TableName returns a pretty table name +func (team20190525101422) TableName() string { + return "teams" +} + +func init() { + migrations = append(migrations, &xormigrate.Migration{ + ID: "20190525101422", + Description: "Add uids to teams", + Migrate: func(tx *xorm.Engine) error { + err := tx.Sync2(team20190525101422{}) + if err != nil { + return err + } + + // get all current teams and generate a random uid for them + var allTeams []*team20190525101422 + err = tx.Find(&allTeams) + if err != nil { + return err + } + + for _, t := range allTeams { + t.UID = utils.MakeRandomString(32) + _, err = tx.Where("id = ?", t.ID).Update(t) + if err != nil { + return err + } + } + + return nil + }, + Rollback: func(tx *xorm.Engine) error { + return dropTableColum(tx, "teams", "uid") + }, + }) +} diff --git a/pkg/models/teams.go b/pkg/models/teams.go index 83f680ccef3..d505a36671f 100644 --- a/pkg/models/teams.go +++ b/pkg/models/teams.go @@ -22,6 +22,8 @@ import "code.vikunja.io/web" type Team struct { // The unique, numeric id of this team. ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"team"` + // The unique uuid for this team. Used to give a team access to things. + UID string `xorm:"varchar(32) not null" json:"uid"` // The name of this team. Name string `xorm:"varchar(250) not null" json:"name" valid:"required,runelength(5|250)" minLength:"5" maxLength:"250"` // The team's description. diff --git a/pkg/models/teams_create.go b/pkg/models/teams_create.go index 09c2f5c29ee..291f5a23ba7 100644 --- a/pkg/models/teams_create.go +++ b/pkg/models/teams_create.go @@ -18,6 +18,7 @@ package models import ( "code.vikunja.io/api/pkg/metrics" + "code.vikunja.io/api/pkg/utils" "code.vikunja.io/web" ) @@ -46,6 +47,7 @@ func (t *Team) Create(a web.Auth) (err error) { t.CreatedByID = doer.ID t.CreatedBy = *doer + t.UID = utils.MakeRandomString(32) _, err = x.Insert(t) if err != nil {