implemented read all method for namespace teams

This commit is contained in:
konrad 2018-07-18 22:06:29 +02:00
parent 6618874441
commit 935c99be45
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
3 changed files with 45 additions and 0 deletions

View file

@ -354,6 +354,22 @@ 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) return fmt.Sprintf("You need to be namespace owner to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
} }
// ErrNeedToHaveNamespaceReadAccess represents an error, where the user is not the owner of that namespace (used i.e. when deleting a namespace)
type ErrNeedToHaveNamespaceReadAccess struct {
NamespaceID int64
UserID int64
}
// IsErrNeedToHaveNamespaceReadAccess checks if an error is a ErrNamespaceDoesNotExist.
func IsErrNeedToHaveNamespaceReadAccess(err error) bool {
_, ok := err.(ErrNeedToHaveNamespaceReadAccess)
return ok
}
func (err ErrNeedToHaveNamespaceReadAccess) Error() string {
return fmt.Sprintf("You need to be namespace owner to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
}
// ============ // ============
// Team errors // Team errors
// ============ // ============

View file

@ -0,0 +1,23 @@
package models
func (tn *TeamNamespace) ReadAll(user *User) (interface{}, error) {
// Check if the user can read the namespace
n, err := GetNamespaceByID(tn.NamespaceID)
if err != nil {
return nil, err
}
if !n.CanRead(user) {
return nil, ErrNeedToHaveNamespaceReadAccess{NamespaceID:tn.NamespaceID, UserID:user.ID}
}
// Get the teams
all := []*Team{}
err = x.Select("teams.*").
Table("teams").
Join("INNER", "team_namespaces", "team_id = teams.id").
Where("team_namespaces.namespace_id = ?", tn.NamespaceID).
Find(&all)
return all, err
}

View file

@ -14,6 +14,12 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.") return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
} }
// Get the object & bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
fmt.Println(err)
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
lists, err := c.CObject.ReadAll(&currentUser) lists, err := c.CObject.ReadAll(&currentUser)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)