From bd2bf24813a1ca19b5feb552c69ccc4d7bae79d5 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 14 Jun 2018 17:43:00 +0200 Subject: [PATCH] Started working on namespaces --- models/namespaces.go | 39 ++++++++++++++++++++++++++++++++++++++- models/teams.go | 10 ++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/models/namespaces.go b/models/namespaces.go index 7e0b7fcf579..ddb9e3fd390 100644 --- a/models/namespaces.go +++ b/models/namespaces.go @@ -7,6 +7,8 @@ type Namespace struct { Description string `xorm:"varchar(700) autoincr not null" json:"description"` OwnerID int64 `xorm:"int(11) autoincr not null" json:"owner_id"` + Owner User `xorm:"-"` + Created int64 `xorm:"created" json:"created"` Updated int64 `xorm:"updated" json:"updated"` } @@ -32,4 +34,39 @@ const ( NamespaceRightWrite // Can manage a namespace, can do everything NamespaceRightAdmin -) \ No newline at end of file +) + +func (user User) IsNamespaceAdmin(namespace Namespace) (ok bool, err error) { + // Owners always have admin rights + if user.ID == namespace.Owner.ID { + return true, nil + } + + // Check if that user is in a team which has admin rights to that namespace + + + return +} + +// CreateOrUpdateNamespace does what it says +func CreateOrUpdateNamespace(namespace *Namespace) (err error) { + // Check if the User exists + _, _, err = GetUserByID(namespace.Owner.ID) + if err != nil { + return + } + + if namespace.ID == 0 { + _, err = x.Insert(namespace) + if err != nil { + return + } + } else { + _, err = x.ID(namespace.ID).Update(namespace) + if err != nil { + return + } + } + + return +} diff --git a/models/teams.go b/models/teams.go index 20a3a7474ac..e47d2ce0bbb 100644 --- a/models/teams.go +++ b/models/teams.go @@ -63,3 +63,13 @@ type TeamList struct { func (TeamList) TableName() string { return "team_list" } + + +func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) { + err = x.Table("teams"). + Join("INNER", "team_namespaces", "teams.id = team_id"). + Where("teams.namespace_id = ?", id). + Find(teams) + + return +} \ No newline at end of file