2018-07-12 21:07:59 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
// IsAdmin returns true or false if the user is admin on that namespace or not
|
|
|
|
func (n *Namespace) IsAdmin(user *User) bool {
|
|
|
|
// Owners always have admin rights
|
|
|
|
if user.ID == n.Owner.ID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:46:34 +02:00
|
|
|
// Check user rights
|
2018-09-06 08:46:47 +02:00
|
|
|
if n.checkUserRights(user, UserRightAdmin) {
|
2018-09-06 08:46:34 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-12 21:07:59 +02:00
|
|
|
// Check if that user is in a team which has admin rights to that namespace
|
2018-07-25 00:49:44 +02:00
|
|
|
return n.checkTeamRights(user, TeamRightAdmin)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanWrite checks if a user has write access to a namespace
|
|
|
|
func (n *Namespace) CanWrite(user *User) bool {
|
2018-09-06 08:46:34 +02:00
|
|
|
// Admins always have write access
|
|
|
|
if n.IsAdmin(user) {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:46:34 +02:00
|
|
|
// Check user rights
|
2018-09-06 08:46:47 +02:00
|
|
|
if n.checkUserRights(user, UserRightWrite) {
|
2018-07-25 00:49:44 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if that user is in a team which has write rights to that namespace
|
|
|
|
return n.checkTeamRights(user, TeamRightWrite)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanRead checks if a user has read access to that namespace
|
|
|
|
func (n *Namespace) CanRead(user *User) bool {
|
2018-09-06 08:46:34 +02:00
|
|
|
// Admins always have read access
|
|
|
|
if n.IsAdmin(user) {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:46:34 +02:00
|
|
|
// Check user rights
|
2018-09-06 08:46:47 +02:00
|
|
|
if n.checkUserRights(user, UserRightRead) {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user is in a team which has access to the namespace
|
2018-07-25 00:49:44 +02:00
|
|
|
return n.checkTeamRights(user, TeamRightRead)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
2018-07-12 23:07:03 +02:00
|
|
|
|
|
|
|
// CanUpdate checks if the user can update the namespace
|
2018-07-21 15:17:02 +02:00
|
|
|
func (n *Namespace) CanUpdate(user *User) bool {
|
2018-10-06 13:25:37 +02:00
|
|
|
nn, err := GetNamespaceByID(n.ID)
|
|
|
|
if err != nil {
|
|
|
|
Log.Error("Error occurred during CanUpdate for Namespace: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-07-12 23:07:03 +02:00
|
|
|
return nn.IsAdmin(user)
|
|
|
|
}
|
2018-07-12 23:16:32 +02:00
|
|
|
|
2018-07-12 23:33:37 +02:00
|
|
|
// CanDelete checks if the user can delete a namespace
|
2018-07-18 08:15:38 +02:00
|
|
|
func (n *Namespace) CanDelete(user *User) bool {
|
2018-10-06 13:25:37 +02:00
|
|
|
nn, err := GetNamespaceByID(n.ID)
|
|
|
|
if err != nil {
|
|
|
|
Log.Error("Error occurred during CanDelete for Namespace: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-07-12 23:33:37 +02:00
|
|
|
return nn.IsAdmin(user)
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:16:32 +02:00
|
|
|
// CanCreate checks if the user can create a new namespace
|
2018-07-18 08:56:19 +02:00
|
|
|
func (n *Namespace) CanCreate(user *User) bool {
|
2018-07-12 23:16:32 +02:00
|
|
|
// This is currently a dummy function, later on we could imagine global limits etc.
|
|
|
|
return true
|
|
|
|
}
|
2018-07-25 00:49:44 +02:00
|
|
|
|
|
|
|
func (n *Namespace) checkTeamRights(user *User, r TeamRight) bool {
|
|
|
|
exists, err := x.Select("namespaces.*").
|
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
2018-09-06 08:46:47 +02:00
|
|
|
Where("namespaces.id = ? "+
|
|
|
|
"AND ((team_members.user_id = ? AND team_namespaces.right = ?) "+
|
2018-09-06 08:46:34 +02:00
|
|
|
"OR namespaces.owner_id = ? ", n.ID, user.ID, r, user.ID).
|
|
|
|
Get(&Namespace{})
|
|
|
|
if err != nil {
|
2018-10-06 13:25:37 +02:00
|
|
|
Log.Error("Error occurred during checkTeamRights for Namespace: %s, TeamRight: %d", err, r)
|
2018-09-06 08:46:34 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespace) checkUserRights(user *User, r UserRight) bool {
|
|
|
|
exists, err := x.Select("namespaces.*").
|
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
2018-09-06 18:52:10 +02:00
|
|
|
Where("namespaces.id = ? AND ("+
|
|
|
|
"namespaces.owner_id = ? "+
|
2018-09-06 08:46:34 +02:00
|
|
|
"OR (users_namespace.user_id = ? AND users_namespace.right = ?))", n.ID, user.ID, user.ID, r).
|
2018-07-25 00:49:44 +02:00
|
|
|
Get(&Namespace{})
|
|
|
|
if err != nil {
|
2018-10-06 13:25:37 +02:00
|
|
|
Log.Error("Error occurred during checkUserRights for Namespace: %s, UserRight: %d", err, r)
|
2018-07-25 00:49:44 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|