2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// User represents a user in the system.
|
|
|
|
type User struct {
|
2017-12-03 02:04:01 +01:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
IsAdmin bool `json:"is_admin,omitempty"`
|
|
|
|
Theme string `json:"theme,omitempty"`
|
|
|
|
Language string `json:"language,omitempty"`
|
|
|
|
Timezone string `json:"timezone,omitempty"`
|
|
|
|
EntryDirection string `json:"entry_sorting_direction,omitempty"`
|
|
|
|
LastLoginAt *time.Time `json:"last_login_at,omitempty"`
|
|
|
|
Extra map[string]string `json:"-"`
|
2017-11-23 07:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUser returns a new User.
|
|
|
|
func NewUser() *User {
|
|
|
|
return &User{Extra: make(map[string]string)}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:44:32 +01:00
|
|
|
// ValidateUserCreation validates new user.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (u User) ValidateUserCreation() error {
|
|
|
|
if err := u.ValidateUserLogin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
return u.ValidatePassword()
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:44:32 +01:00
|
|
|
// ValidateUserModification validates user for modification.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (u User) ValidateUserModification() error {
|
2017-11-26 04:06:02 +01:00
|
|
|
if u.ID <= 0 {
|
|
|
|
return errors.New("The ID is mandatory")
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
if u.Username == "" {
|
|
|
|
return errors.New("The username is mandatory")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := u.ValidatePassword(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
return ValidateTheme(u.Theme)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:44:32 +01:00
|
|
|
// ValidateUserLogin validates user credential requirements.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (u User) ValidateUserLogin() error {
|
|
|
|
if u.Username == "" {
|
|
|
|
return errors.New("The username is mandatory")
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Password == "" {
|
|
|
|
return errors.New("The password is mandatory")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:44:32 +01:00
|
|
|
// ValidatePassword validates user password requirements.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (u User) ValidatePassword() error {
|
|
|
|
if u.Password != "" && len(u.Password) < 6 {
|
|
|
|
return errors.New("The password must have at least 6 characters")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge update the current user with another user.
|
|
|
|
func (u *User) Merge(override *User) {
|
|
|
|
if u.Username != override.Username {
|
|
|
|
u.Username = override.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Password != override.Password {
|
|
|
|
u.Password = override.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.IsAdmin != override.IsAdmin {
|
|
|
|
u.IsAdmin = override.IsAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Theme != override.Theme {
|
|
|
|
u.Theme = override.Theme
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Language != override.Language {
|
|
|
|
u.Language = override.Language
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Timezone != override.Timezone {
|
|
|
|
u.Timezone = override.Timezone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Users represents a list of users.
|
|
|
|
type Users []*User
|