New structure #7

Merged
konrad merged 6 commits from new-structure into master 2018-10-31 12:42:38 +00:00
67 changed files with 226 additions and 333 deletions
Showing only changes of commit 896e3d1fda - Show all commits

View File

@ -1,12 +0,0 @@
package list
import (
"code.vikunja.io/api/pkg/models"
"github.com/go-xorm/xorm"
)
var x *xorm.Engine
func init() {
x = models.GetEngine()
}

View File

@ -2,9 +2,9 @@ package models
// CRUDable defines the crud methods
type CRUDable interface {
Create(*user.User) error
Create(*User) error
ReadOne() error
ReadAll(*user.User) (interface{}, error)
ReadAll(*User) (interface{}, error)
Update() error
Delete() error
}

View File

@ -1,9 +1,7 @@
package list
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user"
)
// List represents a list of tasks
@ -14,14 +12,14 @@ type List struct {
OwnerID int64 `xorm:"int(11) INDEX" json:"-"`
NamespaceID int64 `xorm:"int(11) INDEX" json:"-" param:"namespace"`
Owner user.User `xorm:"-" json:"owner"`
Owner User `xorm:"-" json:"owner"`
Tasks []*ListTask `xorm:"-" json:"tasks"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// GetListsByNamespaceID gets all lists in a namespace
@ -31,9 +29,9 @@ func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
}
// ReadAll gets all lists a user has access to
func (l *List) ReadAll(u *user.User) (interface{}, error) {
func (l *List) ReadAll(u *User) (interface{}, error) {
lists := []*List{}
fullUser, err := user.GetUserByID(u.ID)
fullUser, err := GetUserByID(u.ID)
if err != nil {
return lists, err
}
@ -78,7 +76,7 @@ func (l *List) ReadOne() (err error) {
}
// Get list owner
l.Owner, err = user.GetUserByID(l.OwnerID)
l.Owner, err = GetUserByID(l.OwnerID)
return
}
@ -121,7 +119,7 @@ func AddListDetails(lists []*List) (err error) {
}
// Get all list owners
owners := []*user.User{}
owners := []*User{}
err = x.In("id", ownerIDs).Find(&owners)
if err != nil {
return

View File

@ -1,8 +1,7 @@
package list
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"testing"
)
@ -12,7 +11,7 @@ func TestList_Create(t *testing.T) {
//assert.NoError(t, PrepareTestDatabase())
// Get our doer
doer, err := user.GetUserByID(1)
doer, err := GetUserByID(1)
assert.NoError(t, err)
// Dummy list for testing
@ -80,7 +79,7 @@ func TestList_Create(t *testing.T) {
assert.True(t, errs.IsErrNamespaceDoesNotExist(err))
// Try creating with a nonexistant owner
nUser := &user.User{ID: 9482385}
nUser := &User{ID: 9482385}
err = dummylist.Create(nUser)
assert.Error(t, err)
assert.True(t, errs.IsErrUserDoesNotExist(err))

View File

@ -1,9 +1,7 @@
package list
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/namespace"
"code.vikunja.io/api/pkg/user"
)
// CreateOrUpdateList updates a list or creates it if it doesn't exist
@ -16,7 +14,7 @@ func CreateOrUpdateList(list *List) (err error) {
// Check if the namespace exists
if list.NamespaceID != 0 {
_, err = namespace.GetNamespaceByID(list.NamespaceID)
_, err = GetNamespaceByID(list.NamespaceID)
if err != nil {
return err
}
@ -48,9 +46,9 @@ func (l *List) Update() (err error) {
}
// Create implements the create method of CRUDable
func (l *List) Create(doer *user.User) (err error) {
func (l *List) Create(doer *User) (err error) {
// Check rights
u, err := user.GetUserByID(doer.ID)
u, err := GetUserByID(doer.ID)
if err != nil {
return
}

View File

@ -1,4 +1,4 @@
package list
package models
// Delete implements the delete method of CRUDable
func (l *List) Delete() (err error) {

View File

@ -1,8 +1,7 @@
package list
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
@ -18,7 +17,7 @@ func TestList_ReadAll(t *testing.T) {
assert.Equal(t, len(lists), 2)
// Get all lists our user has access to
u, err := user.GetUserByID(1)
u, err := GetUserByID(1)
assert.NoError(t, err)
lists2 := List{}
@ -29,7 +28,7 @@ func TestList_ReadAll(t *testing.T) {
assert.Equal(t, s.Len(), 1)
// Try getting lists for a nonexistant user
_, err = lists2.ReadAll(&user.User{ID: 984234})
_, err = lists2.ReadAll(&User{ID: 984234})
assert.Error(t, err)
assert.True(t, errs.IsErrUserDoesNotExist(err))
}

View File

@ -1,59 +1,56 @@
package list
package models
import (
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/namespace"
"code.vikunja.io/api/pkg/user"
)
// IsAdmin returns whether the user has admin rights on the list or not
func (l *List) IsAdmin(u *user.User) bool {
func (l *List) IsAdmin(u *User) bool {
// Owners are always admins
if l.Owner.ID == u.ID {
return true
}
// Check individual rights
if l.checkListUserRight(u, models.UserRightAdmin) {
if l.checkListUserRight(u, UserRightAdmin) {
return true
}
return l.checkListTeamRight(u, models.TeamRightAdmin)
return l.checkListTeamRight(u, TeamRightAdmin)
}
// CanWrite return whether the user can write on that list or not
func (l *List) CanWrite(user *user.User) bool {
func (l *List) CanWrite(user *User) bool {
// Admins always have write access
if l.IsAdmin(user) {
return true
}
// Check individual rights
if l.checkListUserRight(user, models.UserRightWrite) {
if l.checkListUserRight(user, UserRightWrite) {
return true
}
return l.checkListTeamRight(user, models.TeamRightWrite)
return l.checkListTeamRight(user, TeamRightWrite)
}
// CanRead checks if a user has read access to a list
func (l *List) CanRead(user *user.User) bool {
func (l *List) CanRead(user *User) bool {
// Admins always have read access
if l.IsAdmin(user) {
return true
}
// Check individual rights
if l.checkListUserRight(user, models.UserRightRead) {
if l.checkListUserRight(user, UserRightRead) {
return true
}
return l.checkListTeamRight(user, models.TeamRightRead)
return l.checkListTeamRight(user, TeamRightRead)
}
// CanDelete checks if the user can delete a list
func (l *List) CanDelete(doer *user.User) bool {
func (l *List) CanDelete(doer *User) bool {
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanDelete for List: %s", err)
return false
@ -62,7 +59,7 @@ func (l *List) CanDelete(doer *user.User) bool {
}
// CanUpdate checks if the user can update a list
func (l *List) CanUpdate(doer *user.User) bool {
func (l *List) CanUpdate(doer *User) bool {
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanUpdate for List: %s", err)
return false
@ -71,13 +68,13 @@ func (l *List) CanUpdate(doer *user.User) bool {
}
// CanCreate checks if the user can update a list
func (l *List) CanCreate(doer *user.User) bool {
func (l *List) CanCreate(doer *User) bool {
// A user can create a list if he has write access to the namespace
n, _ := namespace.GetNamespaceByID(l.NamespaceID)
n, _ := GetNamespaceByID(l.NamespaceID)
return n.CanWrite(doer)
}
func (l *List) checkListTeamRight(user *user.User, r models.TeamRight) bool {
func (l *List) checkListTeamRight(user *User, r TeamRight) bool {
exists, err := x.Select("l.*").
Table("list").
Alias("l").
@ -96,7 +93,7 @@ func (l *List) checkListTeamRight(user *user.User, r models.TeamRight) bool {
return exists
}
func (l *List) checkListUserRight(user *user.User, r models.UserRight) bool {
func (l *List) checkListUserRight(user *User, r UserRight) bool {
exists, err := x.Select("l.*").
Table("list").
Alias("l").

View File

@ -1,9 +1,7 @@
package list
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user"
)
// ListTask represents an task in a todolist
@ -19,10 +17,10 @@ type ListTask struct {
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
CreatedBy user.User `xorm:"-" json:"createdBy"`
CreatedBy User `xorm:"-" json:"createdBy"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName returns the table name for listtasks
@ -58,7 +56,7 @@ func GetTasksByListID(listID int64) (tasks []*ListTask, err error) {
}
}
var users []user.User
var users []User
err = x.In("id", userIDs).Find(&users)
if err != nil {
return
@ -94,7 +92,7 @@ func GetListTaskByID(listTaskID int64) (listTask ListTask, err error) {
return ListTask{}, errs.ErrListTaskDoesNotExist{listTaskID}
}
u, err := user.GetUserByID(listTask.CreatedByID)
u, err := GetUserByID(listTask.CreatedByID)
if err != nil {
return
}

View File

@ -1,13 +1,12 @@
package list
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
"github.com/imdario/mergo"
)
// Create is the implementation to create a list task
func (i *ListTask) Create(doer *user.User) (err error) {
func (i *ListTask) Create(doer *User) (err error) {
i.ID = 0
// Check if we have at least a text
@ -21,7 +20,7 @@ func (i *ListTask) Create(doer *user.User) (err error) {
return
}
u, err := user.GetUserByID(doer.ID)
u, err := GetUserByID(doer.ID)
if err != nil {
return err
}
@ -42,7 +41,7 @@ func (i *ListTask) Update() (err error) {
// For whatever reason, xorm dont detect if done is updated, so we need to update this every time by hand
// Which is why we merge the actual task struct with the one we got from the user.
// The user struct ovverrides values in the actual one.
// The user struct overrides values in the actual one.
if err := mergo.Merge(&ot, i, mergo.WithOverride); err != nil {
return err
}

View File

@ -1,4 +1,4 @@
package list
package models
// Delete implements the delete method for listTask
func (i *ListTask) Delete() (err error) {

View File

@ -1,12 +1,11 @@
package list
package models
import (
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/user"
)
// CanDelete checks if the user can delete an task
func (i *ListTask) CanDelete(doer *user.User) bool {
func (i *ListTask) CanDelete(doer *User) bool {
// Get the task
lI, err := GetListTaskByID(i.ID)
if err != nil {
@ -21,7 +20,7 @@ func (i *ListTask) CanDelete(doer *user.User) bool {
}
// CanUpdate determines if a user has the right to update a list task
func (i *ListTask) CanUpdate(doer *user.User) bool {
func (i *ListTask) CanUpdate(doer *User) bool {
// Get the task
lI, err := GetListTaskByID(i.ID)
if err != nil {
@ -36,7 +35,7 @@ func (i *ListTask) CanUpdate(doer *user.User) bool {
}
// CanCreate determines if a user has the right to create a list task
func (i *ListTask) CanCreate(doer *user.User) bool {
func (i *ListTask) CanCreate(doer *User) bool {
// A user can create an task if he has write acces to its list
l := &List{ID: i.ListID}
l.ReadOne()

View File

@ -1,8 +1,7 @@
package list
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"testing"
)
@ -18,7 +17,7 @@ func TestListTask_Create(t *testing.T) {
}
// Add one point to a list
doer, err := user.GetUserByID(1)
doer, err := GetUserByID(1)
assert.NoError(t, err)
assert.True(t, listtask.CanCreate(&doer))
@ -68,7 +67,7 @@ func TestListTask_Create(t *testing.T) {
assert.True(t, errs.IsErrListTaskDoesNotExist(err))
// Try inserting an task with a nonexistant user
nUser := &user.User{ID: 9482385}
nUser := &User{ID: 9482385}
listtask.ListID = 1
err = listtask.Create(nUser)
assert.Error(t, err)

View File

@ -1,22 +1,17 @@
package sharing
import (
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user"
)
package models
// ListUser represents a list <-> user relation
type ListUser struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"namespace"`
UserID int64 `xorm:"int(11) not null INDEX" json:"user_id" param:"user"`
ListID int64 `xorm:"int(11) not null INDEX" json:"list_id" param:"list"`
Right models.UserRight `xorm:"int(11) INDEX" json:"right"`
Right UserRight `xorm:"int(11) INDEX" json:"right"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName is the table name for ListUser
@ -25,6 +20,6 @@ func (ListUser) TableName() string {
}
type userWithRight struct {
user.User `xorm:"extends"`
Right models.UserRight `json:"right"`
User `xorm:"extends"`
Right UserRight `json:"right"`
}

View File

@ -1,13 +1,11 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/list"
"code.vikunja.io/api/pkg/user"
)
// Create creates a new list <-> user relation
func (ul *ListUser) Create(u *user.User) (err error) {
func (ul *ListUser) Create(u *User) (err error) {
// Check if the right is valid
if err := ul.Right.IsValid(); err != nil {
@ -15,13 +13,13 @@ func (ul *ListUser) Create(u *user.User) (err error) {
}
// Check if the list exists
l := &list.List{ID: ul.ListID}
l := &List{ID: ul.ListID}
if err = l.GetSimpleByID(); err != nil {
return
}
// Check if the user exists
if _, err = user.GetUserByID(ul.UserID); err != nil {
if _, err = GetUserByID(ul.UserID); err != nil {
return err
}

View File

@ -1,15 +1,14 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
)
// Delete deletes a list <-> user relation
func (lu *ListUser) Delete() (err error) {
// Check if the user exists
_, err = user.GetUserByID(lu.UserID)
_, err = GetUserByID(lu.UserID)
if err != nil {
return
}

View File

@ -1,15 +1,13 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/list"
"code.vikunja.io/api/pkg/user"
)
// ReadAll gets all users who have access to a list
func (ul *ListUser) ReadAll(u *user.User) (interface{}, error) {
func (ul *ListUser) ReadAll(u *User) (interface{}, error) {
// Check if the user has access to the list
l := &list.List{ID: ul.ListID}
l := &List{ID: ul.ListID}
if err := l.GetSimpleByID(); err != nil {
return nil, err
}

View File

@ -1,15 +1,13 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/list"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/user"
)
// CanCreate checks if the user can create a new user <-> list relation
func (lu *ListUser) CanCreate(doer *user.User) bool {
func (lu *ListUser) CanCreate(doer *User) bool {
// Get the list and check if the user has write access on it
l := list.List{ID: lu.ListID}
l := List{ID: lu.ListID}
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanCreate for ListUser: %s", err)
return false
@ -18,9 +16,9 @@ func (lu *ListUser) CanCreate(doer *user.User) bool {
}
// CanDelete checks if the user can delete a user <-> list relation
func (lu *ListUser) CanDelete(doer *user.User) bool {
func (lu *ListUser) CanDelete(doer *User) bool {
// Get the list and check if the user has write access on it
l := list.List{ID: lu.ListID}
l := List{ID: lu.ListID}
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanDelete for ListUser: %s", err)
return false
@ -29,9 +27,9 @@ func (lu *ListUser) CanDelete(doer *user.User) bool {
}
// CanUpdate checks if the user can update a user <-> list relation
func (lu *ListUser) CanUpdate(doer *user.User) bool {
func (lu *ListUser) CanUpdate(doer *User) bool {
// Get the list and check if the user has write access on it
l := list.List{ID: lu.ListID}
l := List{ID: lu.ListID}
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanUpdate for ListUser: %s", err)
return false

View File

@ -1,4 +1,4 @@
package sharing
package models
// Update updates a user <-> list relation
func (lu *ListUser) Update() (err error) {

View File

@ -1,10 +1,7 @@
package namespace
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/list"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user"
)
// Namespace holds informations about a namespace
@ -14,13 +11,13 @@ type Namespace struct {
Description string `xorm:"varchar(1000)" json:"description"`
OwnerID int64 `xorm:"int(11) not null INDEX" json:"-"`
Owner user.User `xorm:"-" json:"owner"`
Owner User `xorm:"-" json:"owner"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
@ -45,7 +42,7 @@ func GetNamespaceByID(id int64) (namespace Namespace, err error) {
}
// Get the namespace Owner
namespace.Owner, err = user.GetUserByID(namespace.OwnerID)
namespace.Owner, err = GetUserByID(namespace.OwnerID)
if err != nil {
return namespace, err
}
@ -60,11 +57,11 @@ func (n *Namespace) ReadOne() (err error) {
}
// ReadAll gets all namespaces a user has access to
func (n *Namespace) ReadAll(doer *user.User) (interface{}, error) {
func (n *Namespace) ReadAll(doer *User) (interface{}, error) {
type namespaceWithLists struct {
Namespace `xorm:"extends"`
Lists []*list.List `xorm:"-" json:"lists"`
Lists []*List `xorm:"-" json:"lists"`
}
all := []*namespaceWithLists{}
@ -85,7 +82,7 @@ func (n *Namespace) ReadAll(doer *user.User) (interface{}, error) {
}
// Get all users
users := []*user.User{}
users := []*User{}
err = x.Select("users.*").
Table("namespaces").
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
@ -107,7 +104,7 @@ func (n *Namespace) ReadAll(doer *user.User) (interface{}, error) {
}
// Get all lists
lists := []*list.List{}
lists := []*List{}
err = x.Table(&lists).
In("namespace_id", namespaceids).
Find(&lists)
@ -116,7 +113,7 @@ func (n *Namespace) ReadAll(doer *user.User) (interface{}, error) {
}
// More details for the lists
list.AddListDetails(lists)
AddListDetails(lists)
// Put objects in our namespace list
for i, n := range all {

View File

@ -1,12 +1,11 @@
package namespace
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
)
// Create implements the creation method via the interface
func (n *Namespace) Create(doer *user.User) (err error) {
func (n *Namespace) Create(doer *User) (err error) {
// Check if we have at least a name
if n.Name == "" {
return errs.ErrNamespaceNameCannotBeEmpty{NamespaceID: 0, UserID: doer.ID}
@ -14,7 +13,7 @@ func (n *Namespace) Create(doer *user.User) (err error) {
n.ID = 0 // This would otherwise prevent the creation of new lists after one was created
// Check if the User exists
n.Owner, err = user.GetUserByID(doer.ID)
n.Owner, err = GetUserByID(doer.ID)
if err != nil {
return
}

View File

@ -1,8 +1,4 @@
package namespace
import (
"code.vikunja.io/api/pkg/list"
)
package models
// Delete deletes a namespace
func (n *Namespace) Delete() (err error) {
@ -20,7 +16,7 @@ func (n *Namespace) Delete() (err error) {
}
// Delete all lists with their tasks
lists, err := list.GetListsByNamespaceID(n.ID)
lists, err := GetListsByNamespaceID(n.ID)
var listIDs []int64
// We need to do that for here because we need the list ids to delete two times:
// 1) to delete the lists itself
@ -30,13 +26,13 @@ func (n *Namespace) Delete() (err error) {
}
// Delete tasks
_, err = x.In("list_id", listIDs).Delete(&list.ListTask{})
_, err = x.In("list_id", listIDs).Delete(&ListTask{})
if err != nil {
return
}
// Delete the lists
_, err = x.In("id", listIDs).Delete(&list.List{})
_, err = x.In("id", listIDs).Delete(&List{})
if err != nil {
return
}

View File

@ -1,61 +1,59 @@
package namespace
package models
import (
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user"
)
// IsAdmin returns true or false if the user is admin on that namespace or not
func (n *Namespace) IsAdmin(u *user.User) bool {
func (n *Namespace) IsAdmin(u *User) bool {
// Owners always have admin rights
if u.ID == n.Owner.ID {
return true
}
// Check user rights
if n.checkUserRights(u, models.UserRightAdmin) {
if n.checkUserRights(u, UserRightAdmin) {
return true
}
// Check if that user is in a team which has admin rights to that namespace
return n.checkTeamRights(u, models.TeamRightAdmin)
return n.checkTeamRights(u, TeamRightAdmin)
}
// CanWrite checks if a user has write access to a namespace
func (n *Namespace) CanWrite(u *user.User) bool {
func (n *Namespace) CanWrite(u *User) bool {
// Admins always have write access
if n.IsAdmin(u) {
return true
}
// Check user rights
if n.checkUserRights(u, models.UserRightWrite) {
if n.checkUserRights(u, UserRightWrite) {
return true
}
// Check if that user is in a team which has write rights to that namespace
return n.checkTeamRights(u, models.TeamRightWrite)
return n.checkTeamRights(u, TeamRightWrite)
}
// CanRead checks if a user has read access to that namespace
func (n *Namespace) CanRead(u *user.User) bool {
func (n *Namespace) CanRead(u *User) bool {
// Admins always have read access
if n.IsAdmin(u) {
return true
}
// Check user rights
if n.checkUserRights(u, models.UserRightRead) {
if n.checkUserRights(u, UserRightRead) {
return true
}
// Check if the user is in a team which has access to the namespace
return n.checkTeamRights(u, models.TeamRightRead)
return n.checkTeamRights(u, TeamRightRead)
}
// CanUpdate checks if the user can update the namespace
func (n *Namespace) CanUpdate(u *user.User) bool {
func (n *Namespace) CanUpdate(u *User) bool {
nn, err := GetNamespaceByID(n.ID)
if err != nil {
log.Log.Error("Error occurred during CanUpdate for Namespace: %s", err)
@ -65,7 +63,7 @@ func (n *Namespace) CanUpdate(u *user.User) bool {
}
// CanDelete checks if the user can delete a namespace
func (n *Namespace) CanDelete(u *user.User) bool {
func (n *Namespace) CanDelete(u *User) bool {
nn, err := GetNamespaceByID(n.ID)
if err != nil {
log.Log.Error("Error occurred during CanDelete for Namespace: %s", err)
@ -75,12 +73,12 @@ func (n *Namespace) CanDelete(u *user.User) bool {
}
// CanCreate checks if the user can create a new namespace
func (n *Namespace) CanCreate(u *user.User) bool {
func (n *Namespace) CanCreate(u *User) bool {
// This is currently a dummy function, later on we could imagine global limits etc.
return true
}
func (n *Namespace) checkTeamRights(u *user.User, r models.TeamRight) bool {
func (n *Namespace) checkTeamRights(u *User, r TeamRight) bool {
exists, err := x.Select("namespaces.*").
Table("namespaces").
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
@ -97,7 +95,7 @@ func (n *Namespace) checkTeamRights(u *user.User, r models.TeamRight) bool {
return exists
}
func (n *Namespace) checkUserRights(u *user.User, r models.UserRight) bool {
func (n *Namespace) checkUserRights(u *User, r UserRight) bool {
exists, err := x.Select("namespaces.*").
Table("namespaces").
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").

View File

@ -1,8 +1,7 @@
package namespace
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
@ -19,7 +18,7 @@ func TestNamespace_Create(t *testing.T) {
}
// Doer
doer, err := user.GetUserByID(1)
doer, err := GetUserByID(1)
assert.NoError(t, err)
// Try creating it
@ -41,7 +40,7 @@ func TestNamespace_Create(t *testing.T) {
assert.True(t, errs.IsErrNamespaceNameCannotBeEmpty(err))
// Try inserting one with a nonexistant user
nUser := &user.User{ID: 9482385}
nUser := &User{ID: 9482385}
dnsp2 := dummynamespace
err = dnsp2.Create(nUser)
assert.Error(t, err)

View File

@ -1,8 +1,7 @@
package namespace
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
)
// Update implements the update method via the interface
@ -21,7 +20,7 @@ func (n *Namespace) Update() (err error) {
// Check if the (new) owner exists
n.OwnerID = n.Owner.ID
if currentNamespace.OwnerID != n.OwnerID {
n.Owner, err = user.GetUserByID(n.OwnerID)
n.Owner, err = GetUserByID(n.OwnerID)
if err != nil {
return
}

View File

@ -1,19 +1,17 @@
package sharing
import "code.vikunja.io/api/pkg/models"
package models
// NamespaceUser represents a namespace <-> user relation
type NamespaceUser struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"namespace"`
UserID int64 `xorm:"int(11) not null INDEX" json:"user_id" param:"user"`
NamespaceID int64 `xorm:"int(11) not null INDEX" json:"namespace_id" param:"namespace"`
Right models.UserRight `xorm:"int(11) INDEX" json:"right"`
Right UserRight `xorm:"int(11) INDEX" json:"right"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName is the table name for NamespaceUser

View File

@ -1,13 +1,11 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/namespace"
"code.vikunja.io/api/pkg/user"
)
// Create creates a new namespace <-> user relation
func (un *NamespaceUser) Create(u *user.User) (err error) {
func (un *NamespaceUser) Create(u *User) (err error) {
// Reset the id
un.ID = 0
@ -18,13 +16,13 @@ func (un *NamespaceUser) Create(u *user.User) (err error) {
}
// Check if the namespace exists
l, err := namespace.GetNamespaceByID(un.NamespaceID)
l, err := GetNamespaceByID(un.NamespaceID)
if err != nil {
return
}
// Check if the user exists
if _, err = user.GetUserByID(un.UserID); err != nil {
if _, err = GetUserByID(un.UserID); err != nil {
return err
}

View File

@ -1,15 +1,14 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/user"
)
// Delete deletes a namespace <-> user relation
func (nu *NamespaceUser) Delete() (err error) {
// Check if the user exists
_, err = user.GetUserByID(nu.UserID)
_, err = GetUserByID(nu.UserID)
if err != nil {
return
}

View File

@ -1,15 +1,13 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
"code.vikunja.io/api/pkg/namespace"
"code.vikunja.io/api/pkg/user"
)
// ReadAll gets all users who have access to a namespace
func (un *NamespaceUser) ReadAll(u *user.User) (interface{}, error) {
func (un *NamespaceUser) ReadAll(u *User) (interface{}, error) {
// Check if the user has access to the namespace
l, err := namespace.GetNamespaceByID(un.NamespaceID)
l, err := GetNamespaceByID(un.NamespaceID)
if err != nil {
return nil, err
}

View File

@ -1,15 +1,13 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/namespace"
"code.vikunja.io/api/pkg/user"
)
// CanCreate checks if the user can create a new user <-> namespace relation
func (nu *NamespaceUser) CanCreate(doer *user.User) bool {
func (nu *NamespaceUser) CanCreate(doer *User) bool {
// Get the namespace and check if the user has write access on it
n, err := namespace.GetNamespaceByID(nu.NamespaceID)
n, err := GetNamespaceByID(nu.NamespaceID)
if err != nil {
log.Log.Error("Error occurred during CanCreate for NamespaceUser: %s", err)
return false
@ -18,9 +16,9 @@ func (nu *NamespaceUser) CanCreate(doer *user.User) bool {
}
// CanDelete checks if the user can delete a user <-> namespace relation
func (nu *NamespaceUser) CanDelete(doer *user.User) bool {
func (nu *NamespaceUser) CanDelete(doer *User) bool {
// Get the namespace and check if the user has write access on it
n, err := namespace.GetNamespaceByID(nu.NamespaceID)
n, err := GetNamespaceByID(nu.NamespaceID)
if err != nil {
log.Log.Error("Error occurred during CanDelete for NamespaceUser: %s", err)
return false
@ -29,9 +27,9 @@ func (nu *NamespaceUser) CanDelete(doer *user.User) bool {
}
// CanUpdate checks if the user can update a user <-> namespace relation
func (nu *NamespaceUser) CanUpdate(doer *user.User) bool {
func (nu *NamespaceUser) CanUpdate(doer *User) bool {
// Get the namespace and check if the user has write access on it
n, err := namespace.GetNamespaceByID(nu.NamespaceID)
n, err := GetNamespaceByID(nu.NamespaceID)
if err != nil {
log.Log.Error("Error occurred during CanUpdate for NamespaceUser: %s", err)
return false

View File

@ -1,4 +1,4 @@
package sharing
package models
// Update updates a user <-> namespace relation
func (nu *NamespaceUser) Update() (err error) {

View File

@ -2,10 +2,10 @@ package models
// Rights defines rights methods
type Rights interface {
IsAdmin(*user.User) bool
CanWrite(*user.User) bool
CanRead(*user.User) bool
CanDelete(*user.User) bool
CanUpdate(*user.User) bool
CanCreate(*user.User) bool
IsAdmin(*User) bool
CanWrite(*User) bool
CanRead(*User) bool
CanDelete(*User) bool
CanUpdate(*User) bool
CanCreate(*User) bool
}

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/models"
@ -10,13 +10,13 @@ type TeamList struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null INDEX" json:"team_id" param:"team"`
ListID int64 `xorm:"int(11) not null INDEX" json:"list_id" param:"list"`
Right models.TeamRight `xorm:"int(11) INDEX" json:"right"`
Right TeamRight `xorm:"int(11) INDEX" json:"right"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
@ -26,5 +26,5 @@ func (TeamList) TableName() string {
type teamWithRight struct {
team.Team `xorm:"extends"`
Right models.TeamRight `json:"right"`
Right TeamRight `json:"right"`
}

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -8,7 +8,7 @@ import (
)
// Create creates a new team <-> list relation
func (tl *TeamList) Create(doer *user.User) (err error) {
func (tl *TeamList) Create(doer *User) (err error) {
// Check if the rights are valid
if err = tl.Right.IsValid(); err != nil {

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -7,7 +7,7 @@ import (
)
// ReadAll implements the method to read all teams of a list
func (tl *TeamList) ReadAll(u *user.User) (interface{}, error) {
func (tl *TeamList) ReadAll(u *User) (interface{}, error) {
// Check if the user can read the namespace
l := &list.List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/list"
@ -7,7 +7,7 @@ import (
)
// CanCreate checks if the user can create a team <-> list relation
func (tl *TeamList) CanCreate(u *user.User) bool {
func (tl *TeamList) CanCreate(u *User) bool {
l := list.List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanCreate for TeamList: %s", err)
@ -17,7 +17,7 @@ func (tl *TeamList) CanCreate(u *user.User) bool {
}
// CanDelete checks if the user can delete a team <-> list relation
func (tl *TeamList) CanDelete(user *user.User) bool {
func (tl *TeamList) CanDelete(user *User) bool {
l := list.List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanDelete for TeamList: %s", err)
@ -27,7 +27,7 @@ func (tl *TeamList) CanDelete(user *user.User) bool {
}
// CanUpdate checks if the user can update a team <-> list relation
func (tl *TeamList) CanUpdate(user *user.User) bool {
func (tl *TeamList) CanUpdate(user *User) bool {
l := list.List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
log.Log.Error("Error occurred during CanUpdate for TeamList: %s", err)

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -14,7 +14,7 @@ func TestTeamList(t *testing.T) {
tl := TeamList{
TeamID: 1,
ListID: 1,
Right: models.TeamRightAdmin,
Right: TeamRightAdmin,
}
// Dummyuser
@ -33,7 +33,7 @@ func TestTeamList(t *testing.T) {
// Check with wrong rights
tl2 := tl
tl2.Right = models.TeamRightUnknown
tl2.Right = TeamRightUnknown
err = tl2.Create(&u)
assert.Error(t, err)
assert.True(t, errs.IsErrInvalidTeamRight(err))

View File

@ -1,4 +1,4 @@
package sharing
package models
// Update updates a team <-> list relation
func (tl *TeamList) Update() (err error) {

View File

@ -1,4 +1,4 @@
package team
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -6,7 +6,7 @@ import (
)
// Create implements the create method to assign a user to a team
func (tm *TeamMember) Create(doer *user.User) (err error) {
func (tm *TeamMember) Create(doer *User) (err error) {
// Check if the team extst
_, err = GetTeamByID(tm.TeamID)
if err != nil {

View File

@ -1,4 +1,4 @@
package team
package models
import "code.vikunja.io/api/pkg/errs"

View File

@ -1,4 +1,4 @@
package team
package models
import (
"code.vikunja.io/api/pkg/log"
@ -6,17 +6,17 @@ import (
)
// CanCreate checks if the user can add a new tem member
func (tm *TeamMember) CanCreate(u *user.User) bool {
func (tm *TeamMember) CanCreate(u *User) bool {
return tm.IsAdmin(u)
}
// CanDelete checks if the user can delete a new team member
func (tm *TeamMember) CanDelete(u *user.User) bool {
func (tm *TeamMember) CanDelete(u *User) bool {
return tm.IsAdmin(u)
}
// IsAdmin checks if the user is team admin
func (tm *TeamMember) IsAdmin(u *user.User) bool {
func (tm *TeamMember) IsAdmin(u *User) bool {
// A user can add a member to a team if he is admin of that team
exists, err := x.Where("user_id = ? AND team_id = ? AND admin = ?", u.ID, tm.TeamID, true).
Get(&TeamMember{})

View File

@ -1,4 +1,4 @@
package team
package models
import (
"code.vikunja.io/api/pkg/errs"

View File

@ -1,4 +1,4 @@
package sharing
package models
import "code.vikunja.io/api/pkg/models"
@ -7,13 +7,13 @@ type TeamNamespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null INDEX" json:"team_id" param:"team"`
NamespaceID int64 `xorm:"int(11) not null INDEX" json:"namespace_id" param:"namespace"`
Right models.TeamRight `xorm:"int(11) INDEX" json:"right"`
Right TeamRight `xorm:"int(11) INDEX" json:"right"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -8,7 +8,7 @@ import (
)
// Create creates a new team <-> namespace relation
func (tn *TeamNamespace) Create(doer *user.User) (err error) {
func (tn *TeamNamespace) Create(doer *User) (err error) {
// Check if the rights are valid
if err = tn.Right.IsValid(); err != nil {

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -7,7 +7,7 @@ import (
)
// ReadAll implements the method to read all teams of a namespace
func (tn *TeamNamespace) ReadAll(user *user.User) (interface{}, error) {
func (tn *TeamNamespace) ReadAll(user *User) (interface{}, error) {
// Check if the user can read the namespace
n, err := namespace.GetNamespaceByID(tn.NamespaceID)
if err != nil {

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/log"
@ -7,7 +7,7 @@ import (
)
// CanCreate checks if one can create a new team <-> namespace relation
func (tn *TeamNamespace) CanCreate(user *user.User) bool {
func (tn *TeamNamespace) CanCreate(user *User) bool {
n, err := namespace.GetNamespaceByID(tn.NamespaceID)
if err != nil {
log.Log.Error("Error occurred during CanCreate for TeamNamespace: %s", err)
@ -17,7 +17,7 @@ func (tn *TeamNamespace) CanCreate(user *user.User) bool {
}
// CanDelete checks if a user can remove a team from a namespace. Only namespace admins can do that.
func (tn *TeamNamespace) CanDelete(user *user.User) bool {
func (tn *TeamNamespace) CanDelete(user *User) bool {
n, err := namespace.GetNamespaceByID(tn.NamespaceID)
if err != nil {
log.Log.Error("Error occurred during CanDelete for TeamNamespace: %s", err)
@ -27,7 +27,7 @@ func (tn *TeamNamespace) CanDelete(user *user.User) bool {
}
// CanUpdate checks if a user can update a team from a namespace. Only namespace admins can do that.
func (tn *TeamNamespace) CanUpdate(user *user.User) bool {
func (tn *TeamNamespace) CanUpdate(user *User) bool {
n, err := namespace.GetNamespaceByID(tn.NamespaceID)
if err != nil {
log.Log.Error("Error occurred during CanUpdate for TeamNamespace: %s", err)

View File

@ -1,4 +1,4 @@
package sharing
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -14,7 +14,7 @@ func TestTeamNamespace(t *testing.T) {
tn := TeamNamespace{
TeamID: 1,
NamespaceID: 1,
Right: models.TeamRightAdmin,
Right: TeamRightAdmin,
}
dummyuser, err := user.GetUserByID(1)
@ -32,7 +32,7 @@ func TestTeamNamespace(t *testing.T) {
// Test with invalid team right
tn2 := tn
tn2.Right = models.TeamRightUnknown
tn2.Right = TeamRightUnknown
err = tn2.Create(&dummyuser)
assert.Error(t, err)
assert.True(t, errs.IsErrInvalidTeamRight(err))
@ -64,7 +64,7 @@ func TestTeamNamespace(t *testing.T) {
assert.True(t, errs.IsErrNamespaceDoesNotExist(err))
// Check with no right to read the namespace
nouser := &user.User{ID: 393}
nouser := &User{ID: 393}
_, err = tn.ReadAll(nouser)
assert.Error(t, err)
assert.True(t, errs.IsErrNeedToHaveNamespaceReadAccess(err))

View File

@ -1,4 +1,4 @@
package sharing
package models
// Update updates a team <-> namespace relation
func (tl *TeamNamespace) Update() (err error) {

View File

@ -1,4 +1,4 @@
package team
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -13,14 +13,14 @@ type Team struct {
Description string `xorm:"varchar(250)" json:"description"`
CreatedByID int64 `xorm:"int(11) not null INDEX" json:"-"`
CreatedBy user.User `xorm:"-" json:"created_by"`
CreatedBy User `xorm:"-" json:"created_by"`
Members []*TeamUser `xorm:"-" json:"members"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
@ -51,8 +51,8 @@ type TeamMember struct {
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
models.CRUDable `xorm:"-" json:"-"`
models.Rights `xorm:"-" json:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
@ -62,7 +62,7 @@ func (TeamMember) TableName() string {
// TeamUser is the team member type
type TeamUser struct {
user.User `xorm:"extends"`
User `xorm:"extends"`
Admin bool `json:"admin"`
}
@ -90,7 +90,7 @@ func (t *Team) ReadOne() (err error) {
}
// ReadAll gets all teams the user is part of
func (t *Team) ReadAll(user *user.User) (teams interface{}, err error) {
func (t *Team) ReadAll(user *User) (teams interface{}, err error) {
all := []*Team{}
err = x.Select("teams.*").
Table("teams").

View File

@ -1,4 +1,4 @@
package team
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -6,7 +6,7 @@ import (
)
// Create is the handler to create a team
func (t *Team) Create(doer *user.User) (err error) {
func (t *Team) Create(doer *User) (err error) {
// Check if we have a name
if t.Name == "" {
return errs.ErrTeamNameCannotBeEmpty{}

View File

@ -1,4 +1,4 @@
package team
package models
import "code.vikunja.io/api/pkg/sharing"

View File

@ -1,4 +1,4 @@
package team
package models
import (
"code.vikunja.io/api/pkg/log"
@ -6,13 +6,13 @@ import (
)
// CanCreate checks if the user can create a new team
func (t *Team) CanCreate(u *user.User) bool {
func (t *Team) CanCreate(u *User) bool {
// This is currently a dummy function, later on we could imagine global limits etc.
return true
}
// CanUpdate checks if the user can update a team
func (t *Team) CanUpdate(u *user.User) bool {
func (t *Team) CanUpdate(u *User) bool {
// Check if the current user is in the team and has admin rights in it
exists, err := x.Where("team_id = ?", t.ID).
@ -28,12 +28,12 @@ func (t *Team) CanUpdate(u *user.User) bool {
}
// CanDelete checks if a user can delete a team
func (t *Team) CanDelete(u *user.User) bool {
func (t *Team) CanDelete(u *User) bool {
return t.IsAdmin(u)
}
// IsAdmin returns true when the user is admin of a team
func (t *Team) IsAdmin(u *user.User) bool {
func (t *Team) IsAdmin(u *User) bool {
exists, err := x.Where("team_id = ?", t.ID).
And("user_id = ?", u.ID).
And("admin = ?", true).
@ -46,7 +46,7 @@ func (t *Team) IsAdmin(u *user.User) bool {
}
// CanRead returns true if the user has read access to the team
func (t *Team) CanRead(user *user.User) bool {
func (t *Team) CanRead(user *User) bool {
// Check if the user is in the team
exists, err := x.Where("team_id = ?", t.ID).
And("user_id = ?", user.ID).

View File

@ -1,4 +1,4 @@
package team
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -75,12 +75,12 @@ func TestTeam_Create(t *testing.T) {
}
func TestIsErrInvalidTeamRight(t *testing.T) {
assert.NoError(t, models.TeamRightAdmin.IsValid())
assert.NoError(t, models.TeamRightRead.IsValid())
assert.NoError(t, models.TeamRightWrite.IsValid())
assert.NoError(t, TeamRightAdmin.IsValid())
assert.NoError(t, TeamRightRead.IsValid())
assert.NoError(t, TeamRightWrite.IsValid())
// Check invalid
var tr models.TeamRight
var tr TeamRight
tr = 938
err := tr.IsValid()
assert.Error(t, err)

View File

@ -1,4 +1,4 @@
package team
package models
import "code.vikunja.io/api/pkg/errs"

View File

@ -1,4 +1,4 @@
package user
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -74,7 +74,7 @@ func GetUser(user User) (userOut User, err error) {
}
// CheckUserCredentials checks user credentials
func CheckUserCredentials(u *user.UserLogin) (User, error) {
func CheckUserCredentials(u *UserLogin) (User, error) {
// Check if the user exists
user, err := GetUser(User{Username: u.Username})

View File

@ -1,4 +1,4 @@
package user
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -109,9 +109,9 @@ func UpdateUser(user User) (updatedUser User, err error) {
}
// Check if we have at least a username
if user.Username == "" {
if Username == "" {
//return User{}, ErrNoUsername{user.ID}
user.Username = theUser.Username // Dont change the username if we dont have one
Username = theUser.Username // Dont change the username if we dont have one
}
user.Password = theUser.Password // set the password to the one in the database to not accedently resetting it
@ -132,7 +132,7 @@ func UpdateUser(user User) (updatedUser User, err error) {
}
// UpdateUserPassword updates the password of a user
func UpdateUserPassword(user *user.User, newPassword string) (err error) {
func UpdateUserPassword(user *User, newPassword string) (err error) {
// Get all user details
theUser, err := user.GetUserByID(user.ID)

View File

@ -1,9 +1,9 @@
package user
package models
import "code.vikunja.io/api/pkg/errs"
// DeleteUserByID deletes a user by its ID
func DeleteUserByID(id int64, doer *user.User) error {
func DeleteUserByID(id int64, doer *User) error {
// Check if the id is 0
if id == 0 {
return errs.ErrIDCannotBeZero{}

View File

@ -1,4 +1,4 @@
package user
package models
import "code.vikunja.io/api/pkg/errs"

View File

@ -1,4 +1,4 @@
package user
package models
import (
"code.vikunja.io/api/pkg/errs"

View File

@ -1,4 +1,4 @@
package user
package models
import (
"code.vikunja.io/api/pkg/errs"
@ -27,7 +27,7 @@ func TestCreateUser(t *testing.T) {
assert.NoError(t, err)
// Create a second new user
_, err = CreateUser(User{Username: dummyuser.Username + "2", Email: dummyuser.Email + "m", Password: dummyuser.Password})
_, err = CreateUser(User{Username: dummyUsername + "2", Email: dummyuser.Email + "m", Password: dummyuser.Password})
assert.NoError(t, err)
// Check if it fails to create the same user again
@ -35,7 +35,7 @@ func TestCreateUser(t *testing.T) {
assert.Error(t, err)
// Check if it fails to create a user with just the same username
_, err = CreateUser(User{Username: dummyuser.Username, Password: "fsdf"})
_, err = CreateUser(User{Username: dummyUsername, Password: "fsdf"})
assert.Error(t, err)
assert.True(t, errs.IsErrUsernameExists(err))
@ -65,7 +65,7 @@ func TestCreateUser(t *testing.T) {
// Check the user credentials
user, err := CheckUserCredentials(&UserLogin{"testuu", "1234"})
assert.NoError(t, err)
assert.Equal(t, "testuu", user.Username)
assert.Equal(t, "testuu", Username)
// Check wrong password (should also fail)
_, err = CheckUserCredentials(&UserLogin{"testuu", "12345"})
@ -80,7 +80,7 @@ func TestCreateUser(t *testing.T) {
uuser, err := UpdateUser(User{ID: theuser.ID, Password: "444444"})
assert.NoError(t, err)
assert.Equal(t, theuser.Password, uuser.Password) // Password should not change
assert.Equal(t, theuser.Username, uuser.Username) // Username should not change either
assert.Equal(t, theUsername, uUsername) // Username should not change either
// Try updating one which does not exist
_, err = UpdateUser(User{ID: 99999, Username: "dg"})
@ -93,7 +93,7 @@ func TestCreateUser(t *testing.T) {
assert.NoError(t, err)
// Check if it was changed
user, err = CheckUserCredentials(&UserLogin{theuser.Username, newpassword})
user, err = CheckUserCredentials(&UserLogin{theUsername, newpassword})
assert.NoError(t, err)
// Check if the searchterm works

View File

@ -1,4 +1,4 @@
package user
package models
// ListUsers returns a list with all users, filtered by an optional searchstring
func ListUsers(searchterm string) (users []User, err error) {

View File

@ -1,12 +0,0 @@
package namespace
import (
"code.vikunja.io/api/pkg/models"
"github.com/go-xorm/xorm"
)
var x *xorm.Engine
func init() {
x = models.GetEngine()
}

View File

@ -1,12 +0,0 @@
package sharing
import (
"code.vikunja.io/api/pkg/models"
"github.com/go-xorm/xorm"
)
var x *xorm.Engine
func init() {
x = models.GetEngine()
}

View File

@ -1,12 +0,0 @@
package team
import (
"code.vikunja.io/api/pkg/models"
"github.com/go-xorm/xorm"
)
var x *xorm.Engine
func init() {
x = models.GetEngine()
}

View File

@ -1,12 +0,0 @@
package user
import (
"code.vikunja.io/api/pkg/models"
"github.com/go-xorm/xorm"
)
var x *xorm.Engine
func init() {
x = models.GetEngine()
}