1
0

implemented namespace update via interface method

This commit is contained in:
konrad
2018-07-12 00:30:31 +02:00
committed by kolaente
parent 0aa84e653f
commit 261aaba315
9 changed files with 103 additions and 224 deletions

View File

@ -320,4 +320,36 @@ func IsErrNamespaceNameCannotBeEmpty(err error) bool {
func (err ErrNamespaceNameCannotBeEmpty) Error() string {
return fmt.Sprintf("Namespace name cannot be emtpy [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
}
}
// ErrNamespaceOwnerCannotBeEmpty represents an error, where a namespace owner is empty.
type ErrNamespaceOwnerCannotBeEmpty struct {
NamespaceID int64
UserID int64
}
// IsErrNamespaceOwnerCannotBeEmpty checks if an error is a ErrNamespaceDoesNotExist.
func IsErrNamespaceOwnerCannotBeEmpty(err error) bool {
_, ok := err.(ErrNamespaceOwnerCannotBeEmpty)
return ok
}
func (err ErrNamespaceOwnerCannotBeEmpty) Error() string {
return fmt.Sprintf("Namespace owner cannot be emtpy [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
}
// ErrNeedToBeNamespaceAdmin represents an error, where the user is not the admin of that namespace (used i.e. when deleting a namespace)
type ErrNeedToBeNamespaceAdmin struct {
NamespaceID int64
UserID int64
}
// IsErrNeedToBeNamespaceAdmin checks if an error is a ErrNamespaceDoesNotExist.
func IsErrNeedToBeNamespaceAdmin(err error) bool {
_, ok := err.(ErrNeedToBeNamespaceAdmin)
return ok
}
func (err ErrNeedToBeNamespaceAdmin) Error() string {
return fmt.Sprintf("You need to be namespace owner to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
}

View File

@ -51,6 +51,19 @@ func (user *User) IsNamespaceAdmin(namespace *Namespace) (err error) {
return ErrUserNeedsToBeNamespaceAdmin{UserID: user.ID, NamespaceID: namespace.ID}
}
func (n *Namespace) IsAdmin(user *User) bool {
// Owners always have admin rights
if user.ID == n.Owner.ID {
return true
}
// Check if that user is in a team which has admin rights to that namespace
// TODO
return false
}
// HasNamespaceAccess checks if the User has namespace read access
func (user *User) HasNamespaceAccess(namespace *Namespace) (err error) {
// Owners always have access
@ -65,26 +78,14 @@ func (user *User) HasNamespaceAccess(namespace *Namespace) (err error) {
// CanWrite checks if a user has write access to a namespace
func (n *Namespace) CanWrite(user *User) bool {
if err := user.HasNamespaceAccess(n); err != nil {
return false
// Owners always have access
if user.ID == n.Owner.ID {
return true
}
return true
}
// HasNamespaceWriteAccess checks if a user has write access to a namespace
func (user *User) HasNamespaceWriteAccess(namespace *Namespace) (err error) {
// Owners always have access
if user.ID == namespace.Owner.ID {
return nil
}
// Check if the user is in a team which has write access to the namespace
return ErrUserDoesNotHaveAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID}
}
// GetNamespaceByID returns a namespace object by its ID
func GetNamespaceByID(id int64) (namespace Namespace, err error) {
namespace.ID = id

View File

@ -1,36 +1,6 @@
package models
// CreateOrUpdateNamespace does what it says
func CreateOrUpdateNamespace(namespace *Namespace) (err error) {
// Check if the namespace exists
_, err = GetNamespaceByID(namespace.ID)
if err != nil {
return
}
// Check if the User exists
namespace.Owner, _, err = GetUserByID(namespace.Owner.ID)
if err != nil {
return
}
namespace.OwnerID = namespace.Owner.ID
if namespace.ID == 0 {
_, err = x.Insert(namespace)
} else {
_, err = x.ID(namespace.ID).Update(namespace)
}
if err != nil {
return
}
// Get the new one
*namespace, err = GetNamespaceByID(namespace.ID)
return
}
// Create implements the creation method via the interface
func (n *Namespace) Create(doer *User, _ int64) (err error) {
// Check if we have at least a name
if n.Name == "" {
@ -48,3 +18,40 @@ func (n *Namespace) Create(doer *User, _ int64) (err error) {
_, err = x.Insert(n)
return
}
// Update implements the update method via the interface
func (n *Namespace) Update(id int64, doer *User) (err error) {
// Check if we have at least a name
if n.Name == "" {
return ErrNamespaceNameCannotBeEmpty{NamespaceID:id, UserID:doer.ID}
}
n.ID = id
// Check if the namespace exists
currentNamespace, err := GetNamespaceByID(id)
if err != nil {
return
}
// Check if the (new) owner exists
if currentNamespace.OwnerID != n.OwnerID {
n.Owner, _, err = GetUserByID(doer.ID)
if err != nil {
return
}
}
// Check rights
user, _, err := GetUserByID(doer.ID)
if err != nil {
return
}
if !currentNamespace.IsAdmin(&user) {
return ErrNeedToBeNamespaceAdmin{NamespaceID:id, UserID:doer.ID}
}
// Do the actual update
_, err = x.ID(currentNamespace.ID).Update(n)
return
}

View File

@ -51,7 +51,8 @@ func CreateUser(user User) (newUser User, err error) {
}
// Create the user's namespace
err = CreateOrUpdateNamespace(&Namespace{Name: newUserOut.Username, Description: newUserOut.Username + "'s namespace.", Owner: newUserOut})
newN := &Namespace{Name: newUserOut.Username, Description: newUserOut.Username + "'s namespace.", Owner: newUserOut}
err = newN.Create(&newUserOut, 0)
if err != nil {
return User{}, err
}