2017-11-20 00:10:04 -05: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.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package model // import "miniflux.app/model"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
2018-03-04 20:04:31 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/timezone"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// User represents a user in the system.
|
|
|
|
type User struct {
|
2017-12-02 20:04:01 -05:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password,omitempty"`
|
2017-12-29 16:53:02 -05:00
|
|
|
IsAdmin bool `json:"is_admin"`
|
|
|
|
Theme string `json:"theme"`
|
|
|
|
Language string `json:"language"`
|
|
|
|
Timezone string `json:"timezone"`
|
|
|
|
EntryDirection string `json:"entry_sorting_direction"`
|
2017-12-02 20:04:01 -05:00
|
|
|
LastLoginAt *time.Time `json:"last_login_at,omitempty"`
|
2017-12-29 16:53:02 -05:00
|
|
|
Extra map[string]string `json:"extra"`
|
2017-11-23 01:22:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUser returns a new User.
|
|
|
|
func NewUser() *User {
|
|
|
|
return &User{Extra: make(map[string]string)}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-25 15:44:32 -05:00
|
|
|
// ValidateUserCreation validates new user.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (u User) ValidateUserCreation() error {
|
|
|
|
if err := u.ValidateUserLogin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
return u.ValidatePassword()
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-24 21:04:34 -05:00
|
|
|
// ValidateUserModification validates user modification payload.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (u User) ValidateUserModification() error {
|
2017-12-24 21:04:34 -05:00
|
|
|
if u.Theme != "" {
|
|
|
|
return ValidateTheme(u.Theme)
|
2017-11-25 22:06:02 -05:00
|
|
|
}
|
|
|
|
|
2017-12-24 21:04:34 -05:00
|
|
|
if u.Password != "" {
|
|
|
|
return u.ValidatePassword()
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-24 21:04:34 -05:00
|
|
|
return nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-25 15:44:32 -05:00
|
|
|
// ValidateUserLogin validates user credential requirements.
|
2017-11-20 00:10:04 -05: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 15:44:32 -05:00
|
|
|
// ValidatePassword validates user password requirements.
|
2017-11-20 00:10:04 -05: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
|
|
|
|
}
|
|
|
|
|
2018-03-04 20:04:31 -05:00
|
|
|
// UseTimezone converts last login date to the given timezone.
|
|
|
|
func (u *User) UseTimezone(tz string) {
|
|
|
|
if u.LastLoginAt != nil {
|
|
|
|
*u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
// Users represents a list of users.
|
|
|
|
type Users []*User
|
2018-03-04 20:04:31 -05:00
|
|
|
|
|
|
|
// UseTimezone converts last login timestamp of all users to the given timezone.
|
|
|
|
func (u Users) UseTimezone(tz string) {
|
|
|
|
for _, user := range u {
|
|
|
|
user.UseTimezone(tz)
|
|
|
|
}
|
|
|
|
}
|