Fixed lint + fmt
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
package models
|
||||
|
||||
// CRUDable defines the crud methods
|
||||
type CRUDable interface {
|
||||
Create(*User) (error)
|
||||
Create(*User) error
|
||||
ReadOne(int64) error
|
||||
ReadAll(*User) (interface{}, error)
|
||||
Update(int64, *User) error
|
||||
|
@ -149,7 +149,7 @@ type ErrNeedToBeListAdmin struct {
|
||||
UserID int64
|
||||
}
|
||||
|
||||
// IsErrListDoesNotExist checks if an error is a ErrListDoesNotExist.
|
||||
// IsErrNeedToBeListAdmin checks if an error is a ErrListDoesNotExist.
|
||||
func IsErrNeedToBeListAdmin(err error) bool {
|
||||
_, ok := err.(ErrNeedToBeListAdmin)
|
||||
return ok
|
||||
@ -176,12 +176,12 @@ func (err ErrListItemCannotBeEmpty) Error() string {
|
||||
return fmt.Sprintf("List item text cannot be empty.")
|
||||
}
|
||||
|
||||
// ErrListItemCannotBeEmpty represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
|
||||
// ErrListItemDoesNotExist represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
|
||||
type ErrListItemDoesNotExist struct {
|
||||
ID int64
|
||||
}
|
||||
|
||||
// IsErrListItemCannotBeEmpty checks if an error is a ErrListDoesNotExist.
|
||||
// IsErrListItemDoesNotExist checks if an error is a ErrListDoesNotExist.
|
||||
func IsErrListItemDoesNotExist(err error) bool {
|
||||
_, ok := err.(ErrListItemDoesNotExist)
|
||||
return ok
|
||||
@ -232,7 +232,7 @@ type ErrNeedToBeNamespaceOwner struct {
|
||||
UserID int64
|
||||
}
|
||||
|
||||
// IsErrNamespaceDoesNotExist checks if an error is a ErrNamespaceDoesNotExist.
|
||||
// IsErrNeedToBeNamespaceOwner checks if an error is a ErrNamespaceDoesNotExist.
|
||||
func IsErrNeedToBeNamespaceOwner(err error) bool {
|
||||
_, ok := err.(ErrNeedToBeNamespaceOwner)
|
||||
return ok
|
||||
@ -289,4 +289,3 @@ func IsErrUserDoesNotHaveWriteAccessToNamespace(err error) bool {
|
||||
func (err ErrUserDoesNotHaveWriteAccessToNamespace) Error() string {
|
||||
return fmt.Sprintf("You need to have write access to this namespace to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/labstack/echo"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetIntURLParam is a helper method which returns an int from an url param
|
||||
func GetIntURLParam(param string, c echo.Context) (intParam int64, err error) {
|
||||
|
||||
id := c.Param(param)
|
||||
@ -16,6 +16,7 @@ func GetIntURLParam(param string, c echo.Context) (intParam int64, err error) {
|
||||
return intParam, err
|
||||
}
|
||||
|
||||
// GetByID gets an object by its ID
|
||||
func GetByID(id int64, result interface{}) (err error) {
|
||||
exists, err := x.ID(id).Get(result)
|
||||
if err != nil {
|
||||
@ -28,9 +29,3 @@ func GetByID(id int64, result interface{}) (err error) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetAllByUser(user *User, result interface{}) (err error) {
|
||||
fmt.Println(result)
|
||||
err = x.Where("owner_id = ", user.ID).Find(result)
|
||||
return
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ func CreateOrUpdateList(list *List) (err error) {
|
||||
|
||||
}
|
||||
|
||||
func (l *List) Update(id int64, doer *User) (err error) {
|
||||
// Update implements the update method of CRUDable
|
||||
func (l *List) Update(id int64, doer *User) (err error) {
|
||||
l.ID = id
|
||||
|
||||
// Check if it exists
|
||||
@ -35,12 +36,13 @@ func (l *List) Update(id int64, doer *User) (err error) {
|
||||
}
|
||||
|
||||
if !oldList.IsAdmin(&user) {
|
||||
return ErrNeedToBeListAdmin{ListID:id, UserID:user.ID}
|
||||
return ErrNeedToBeListAdmin{ListID: id, UserID: user.ID}
|
||||
}
|
||||
|
||||
return CreateOrUpdateList(l)
|
||||
}
|
||||
|
||||
// Create implements the create method of CRUDable
|
||||
func (l *List) Create(doer *User) (err error) {
|
||||
// Check rights
|
||||
user, _, err := GetUserByID(doer.ID)
|
||||
@ -54,10 +56,10 @@ func (l *List) Create(doer *User) (err error) {
|
||||
return
|
||||
}
|
||||
if !namespace.CanWrite(doer) {
|
||||
return ErrUserDoesNotHaveWriteAccessToNamespace{UserID:user.ID, NamespaceID:namespace.ID}
|
||||
return ErrUserDoesNotHaveWriteAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID}
|
||||
}
|
||||
|
||||
l.Owner.ID = user.ID
|
||||
|
||||
return CreateOrUpdateList(l)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package models
|
||||
|
||||
// Delete implements the delete method of CRUDable
|
||||
func (l *List) Delete(id int64, doer *User) (err error) {
|
||||
// Check if the list exists
|
||||
list, err := GetListByID(id)
|
||||
@ -14,7 +15,7 @@ func (l *List) Delete(id int64, doer *User) (err error) {
|
||||
}
|
||||
|
||||
if !list.IsAdmin(&user) {
|
||||
return ErrNeedToBeListAdmin{ListID:id, UserID:user.ID}
|
||||
return ErrNeedToBeListAdmin{ListID: id, UserID: user.ID}
|
||||
}
|
||||
|
||||
// Delete the list
|
||||
|
@ -70,6 +70,7 @@ func GetItemsByListID(listID int64) (items []*ListItem, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// GetListItemByID returns all items a list has
|
||||
func GetListItemByID(listItemID int64) (listItem ListItem, err error) {
|
||||
exists, err := x.ID(listItemID).Get(&listItem)
|
||||
if err != nil {
|
||||
|
@ -15,7 +15,7 @@ type List struct {
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
||||
CRUDable `xorm:"-" json:"-"`
|
||||
Rights `xorm:"-" json:"-"`
|
||||
Rights `xorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// Lists is a multiple of list
|
||||
@ -45,13 +45,14 @@ func GetListByID(id int64) (list List, err error) {
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// GetListsByNamespaceID gets all lists in a namespace
|
||||
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
|
||||
err = x.Where("namespace_id = ?", nID).Find(&lists)
|
||||
return lists, err
|
||||
}
|
||||
|
||||
// ReadAll gets all List a user has access to
|
||||
func (list *List) ReadAll(user *User) (interface{}, error) {
|
||||
func (l *List) ReadAll(user *User) (interface{}, error) {
|
||||
lists := Lists{}
|
||||
fullUser, _, err := GetUserByID(user.ID)
|
||||
if err != nil {
|
||||
@ -75,6 +76,7 @@ func (l *List) ReadOne(id int64) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// IsAdmin returns whether the user has admin rights on the list or not
|
||||
func (l *List) IsAdmin(user *User) bool {
|
||||
// Owners are always admins
|
||||
if l.Owner.ID == user.ID {
|
||||
@ -89,4 +91,4 @@ func (l *List) IsAdmin(user *User) bool {
|
||||
// TODO
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package models
|
||||
|
||||
// DeleteNamespaceByID deletes a namespace and takes its id as an argument
|
||||
func DeleteNamespaceByID(namespaceID int64, doer *User) (err error) {
|
||||
|
||||
// Check if the namespace exists
|
||||
|
@ -13,7 +13,7 @@ type Namespace struct {
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
||||
CRUDable `xorm:"-" json:"-"`
|
||||
Rights `xorm:"-" json:"-"`
|
||||
Rights `xorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// TableName makes beautiful table names
|
||||
@ -39,6 +39,7 @@ const (
|
||||
NamespaceRightAdmin
|
||||
)
|
||||
|
||||
// IsNamespaceAdmin returns whether the usre has admin rights in a namespace
|
||||
func (user *User) IsNamespaceAdmin(namespace *Namespace) (err error) {
|
||||
// Owners always have admin rights
|
||||
if user.ID == namespace.Owner.ID {
|
||||
@ -50,6 +51,7 @@ func (user *User) IsNamespaceAdmin(namespace *Namespace) (err error) {
|
||||
return ErrUserNeedsToBeNamespaceAdmin{UserID: user.ID, NamespaceID: namespace.ID}
|
||||
}
|
||||
|
||||
// HasNamespaceAccess checks if the User has namespace read access
|
||||
func (user *User) HasNamespaceAccess(namespace *Namespace) (err error) {
|
||||
// Owners always have access
|
||||
if user.ID == namespace.Owner.ID {
|
||||
@ -61,6 +63,7 @@ func (user *User) HasNamespaceAccess(namespace *Namespace) (err error) {
|
||||
return ErrUserDoesNotHaveAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID}
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -69,6 +72,7 @@ func (n *Namespace) CanWrite(user *User) bool {
|
||||
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
|
||||
@ -81,6 +85,7 @@ func (user *User) HasNamespaceWriteAccess(namespace *Namespace) (err error) {
|
||||
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
|
||||
exists, err := x.Get(&namespace)
|
||||
|
@ -1,5 +1,6 @@
|
||||
package models
|
||||
|
||||
// Rights defines rights methods
|
||||
type Rights interface {
|
||||
IsAdmin(*User) bool
|
||||
CanWrite(*User) bool
|
||||
|
@ -34,7 +34,7 @@ func (TeamMember) TableName() string {
|
||||
return "team_members"
|
||||
}
|
||||
|
||||
// TeamNamespaces defines the relationship between a Team and a Namespace
|
||||
// TeamNamespace defines the relationship between a Team and a Namespace
|
||||
type TeamNamespace struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
||||
TeamID int64 `xorm:"int(11) autoincr not null"`
|
||||
@ -64,6 +64,7 @@ func (TeamList) TableName() string {
|
||||
return "team_list"
|
||||
}
|
||||
|
||||
// GetAllTeamsByNamespaceID returns all teams for a namespace
|
||||
func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
|
||||
err = x.Table("teams").
|
||||
Join("INNER", "team_namespaces", "teams.id = team_id").
|
||||
|
@ -27,15 +27,16 @@ func (User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
// ApiUserPassword represents a user object without timestamps and a json password field.
|
||||
type ApiUserPassword struct {
|
||||
// APIUserPassword represents a user object without timestamps and a json password field.
|
||||
type APIUserPassword struct {
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func (apiUser *ApiUserPassword) APIFormat() User {
|
||||
// APIFormat formats an API User into a normal user struct
|
||||
func (apiUser *APIUserPassword) APIFormat() User {
|
||||
return User{
|
||||
ID: apiUser.ID,
|
||||
Username: apiUser.Username,
|
||||
|
Reference in New Issue
Block a user