2020-12-29 02:04:20 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
2019-12-04 20:39:56 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-12-29 02:04:20 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
package user
|
2018-06-10 11:11:41 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2021-07-30 15:01:04 +02:00
|
|
|
"strconv"
|
2020-10-11 22:10:03 +02:00
|
|
|
"time"
|
|
|
|
|
2021-02-17 20:48:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/config"
|
2021-02-14 23:11:39 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
2021-07-30 15:01:04 +02:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
|
|
|
"code.vikunja.io/api/pkg/modules/keyvalue"
|
|
|
|
"code.vikunja.io/api/pkg/notifications"
|
2020-12-23 16:32:28 +01:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
"code.vikunja.io/web"
|
2021-08-03 23:43:18 +02:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2019-05-07 21:42:24 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-06-10 11:11:41 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-07-30 15:01:04 +02:00
|
|
|
"xorm.io/xorm"
|
2018-06-10 11:11:41 +02:00
|
|
|
)
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
// Login Object to recive user credentials in JSON format
|
|
|
|
type Login struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The username used to log in.
|
2019-01-14 23:32:56 +01:00
|
|
|
Username string `json:"username"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The password for the user.
|
2019-01-14 23:32:56 +01:00
|
|
|
Password string `json:"password"`
|
2020-04-17 21:25:35 +02:00
|
|
|
// The totp passcode of a user. Only needs to be provided when enabled.
|
|
|
|
TOTPPasscode string `json:"totp_passcode"`
|
2022-02-06 14:18:08 +01:00
|
|
|
// If true, the token returned will be valid a lot longer than default. Useful for "remember me" style logins.
|
|
|
|
LongToken bool `json:"long_token"`
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2021-07-13 22:56:02 +02:00
|
|
|
type Status int
|
|
|
|
|
|
|
|
func (s Status) String() string {
|
|
|
|
switch s {
|
|
|
|
case StatusActive:
|
|
|
|
return "Active"
|
|
|
|
case StatusEmailConfirmationRequired:
|
|
|
|
return "Email Confirmation required"
|
|
|
|
case StatusDisabled:
|
|
|
|
return "Disabled"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
StatusActive = iota
|
|
|
|
StatusEmailConfirmationRequired
|
|
|
|
StatusDisabled
|
|
|
|
)
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// User holds information about an user
|
|
|
|
type User struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this user.
|
2020-12-18 17:51:22 +01:00
|
|
|
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id"`
|
2020-11-21 21:51:55 +01:00
|
|
|
// The full name of the user.
|
|
|
|
Name string `xorm:"text null" json:"name"`
|
2019-01-14 23:32:56 +01:00
|
|
|
// The username of the user. Is always unique.
|
2020-09-26 23:02:17 +02:00
|
|
|
Username string `xorm:"varchar(250) not null unique" json:"username" valid:"length(1|250)" minLength:"1" maxLength:"250"`
|
2020-11-21 17:38:58 +01:00
|
|
|
Password string `xorm:"varchar(250) null" json:"-"`
|
2019-01-14 23:32:56 +01:00
|
|
|
// The user's email address.
|
2021-07-13 22:56:02 +02:00
|
|
|
Email string `xorm:"varchar(250) null" json:"email,omitempty" valid:"email,length(0|250)" maxLength:"250"`
|
2018-10-27 11:33:28 +02:00
|
|
|
|
2021-07-13 22:56:02 +02:00
|
|
|
Status Status `xorm:"default 0" json:"-"`
|
2018-10-27 11:33:28 +02:00
|
|
|
|
2020-08-02 19:16:58 +02:00
|
|
|
AvatarProvider string `xorm:"varchar(255) null" json:"-"`
|
2021-03-23 22:32:23 +01:00
|
|
|
AvatarFileID int64 `xorm:"null" json:"-"`
|
2020-08-02 19:16:58 +02:00
|
|
|
|
2020-11-21 17:38:58 +01:00
|
|
|
// Issuer and Subject contain the issuer and subject from the source the user authenticated with.
|
|
|
|
Issuer string `xorm:"text null" json:"-"`
|
|
|
|
Subject string `xorm:"text null" json:"-"`
|
|
|
|
|
2021-12-12 15:39:47 +01:00
|
|
|
EmailRemindersEnabled bool `xorm:"bool default true" json:"-"`
|
|
|
|
DiscoverableByName bool `xorm:"bool default false index" json:"-"`
|
|
|
|
DiscoverableByEmail bool `xorm:"bool default false index" json:"-"`
|
|
|
|
OverdueTasksRemindersEnabled bool `xorm:"bool default true index" json:"-"`
|
|
|
|
DefaultListID int64 `xorm:"bigint null index" json:"-"`
|
|
|
|
WeekStart int `xorm:"null" json:"-"`
|
|
|
|
Language string `xorm:"varchar(50) null" json:"-"`
|
2022-01-16 12:05:56 +01:00
|
|
|
Timezone string `xorm:"varchar(255) null" json:"-"`
|
2021-04-07 18:28:58 +02:00
|
|
|
|
2021-08-11 21:08:10 +02:00
|
|
|
DeletionScheduledAt time.Time `xorm:"datetime null" json:"-"`
|
|
|
|
DeletionLastReminderSent time.Time `xorm:"datetime null" json:"-"`
|
|
|
|
|
2021-09-04 21:26:31 +02:00
|
|
|
ExportFileID int64 `xorm:"bigint null" json:"-"`
|
|
|
|
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this task was created. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Created time.Time `xorm:"created not null" json:"created"`
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this task was last updated. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Updated time.Time `xorm:"updated not null" json:"updated"`
|
2018-12-01 00:26:56 +01:00
|
|
|
|
|
|
|
web.Auth `xorm:"-" json:"-"`
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 22:05:09 +01:00
|
|
|
// RouteForMail routes all notifications for a user to its email address
|
2021-02-14 22:44:01 +01:00
|
|
|
func (u *User) RouteForMail() (string, error) {
|
|
|
|
|
|
|
|
if u.Email == "" {
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
user, err := getUser(s, &User{ID: u.ID}, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return user.Email, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Email, nil
|
2021-02-07 22:05:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteForDB routes all notifications for a user to their id
|
|
|
|
func (u *User) RouteForDB() int64 {
|
|
|
|
return u.ID
|
|
|
|
}
|
|
|
|
|
2019-06-28 10:21:48 +02:00
|
|
|
// GetID implements the Auth interface
|
|
|
|
func (u *User) GetID() int64 {
|
|
|
|
return u.ID
|
|
|
|
}
|
2018-12-01 00:26:56 +01:00
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// TableName returns the table name for users
|
|
|
|
func (User) TableName() string {
|
|
|
|
return "users"
|
|
|
|
}
|
|
|
|
|
2021-02-07 22:05:09 +01:00
|
|
|
// GetName returns the name if the user has one and the username otherwise.
|
|
|
|
func (u *User) GetName() string {
|
|
|
|
if u.Name != "" {
|
|
|
|
return u.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Username
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:48:06 +01:00
|
|
|
// GetNameAndFromEmail returns the name and email address for a user. Useful to use in notifications.
|
|
|
|
func (u *User) GetNameAndFromEmail() string {
|
|
|
|
return u.GetName() + " via Vikunja <" + config.MailerFromEmail.GetString() + ">"
|
|
|
|
}
|
|
|
|
|
2021-07-30 15:01:04 +02:00
|
|
|
func (u *User) GetFailedTOTPAttemptsKey() string {
|
|
|
|
return "failed_totp_attempts_" + strconv.FormatInt(u.ID, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GetFailedPasswordAttemptsKey() string {
|
|
|
|
return "failed_password_attempts_" + strconv.FormatInt(u.ID, 10)
|
|
|
|
}
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
// GetFromAuth returns a user object from a web.Auth object and returns an error if the underlying type
|
|
|
|
// is not a user object
|
|
|
|
func GetFromAuth(a web.Auth) (*User, error) {
|
2018-12-01 00:26:56 +01:00
|
|
|
u, is := a.(*User)
|
|
|
|
if !is {
|
|
|
|
return &User{}, fmt.Errorf("user is not user element, is %s", reflect.TypeOf(a))
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// APIUserPassword represents a user object without timestamps and a json password field.
|
|
|
|
type APIUserPassword struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this user.
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
// The username of the username. Is always unique.
|
|
|
|
Username string `json:"username" valid:"length(3|250)" minLength:"3" maxLength:"250"`
|
|
|
|
// The user's password in clear text. Only used when registering the user.
|
|
|
|
Password string `json:"password" valid:"length(8|250)" minLength:"8" maxLength:"250"`
|
|
|
|
// The user's email address
|
|
|
|
Email string `json:"email" valid:"email,length(0|250)" maxLength:"250"`
|
2018-06-13 13:45:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// APIFormat formats an API User into a normal user struct
|
2019-08-14 21:59:31 +02:00
|
|
|
func (apiUser *APIUserPassword) APIFormat() *User {
|
|
|
|
return &User{
|
2018-06-13 13:45:22 +02:00
|
|
|
ID: apiUser.ID,
|
|
|
|
Username: apiUser.Username,
|
|
|
|
Password: apiUser.Password,
|
|
|
|
Email: apiUser.Email,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// GetUserByID gets informations about a user by its ID
|
2020-12-23 16:32:28 +01:00
|
|
|
func GetUserByID(s *xorm.Session, id int64) (user *User, err error) {
|
2018-06-10 11:11:41 +02:00
|
|
|
// Apparently xorm does otherwise look for all users but return only one, which leads to returing one even if the ID is 0
|
2018-09-13 20:07:11 +02:00
|
|
|
if id < 1 {
|
2019-08-14 21:59:31 +02:00
|
|
|
return &User{}, ErrUserDoesNotExist{}
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
return getUser(s, &User{ID: id}, false)
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-25 11:47:16 +02:00
|
|
|
// GetUserByUsername gets a user from its user name. This is an extra function to be able to add an extra error check.
|
2020-12-23 16:32:28 +01:00
|
|
|
func GetUserByUsername(s *xorm.Session, username string) (user *User, err error) {
|
2019-05-25 11:47:16 +02:00
|
|
|
if username == "" {
|
2019-08-14 21:59:31 +02:00
|
|
|
return &User{}, ErrUserDoesNotExist{}
|
2019-05-25 11:47:16 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
return getUser(s, &User{Username: username}, false)
|
2019-08-14 21:59:31 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 17:42:49 +02:00
|
|
|
// GetUsersByUsername returns a slice of users with the provided usernames
|
|
|
|
func GetUsersByUsername(s *xorm.Session, usernames []string, withEmails bool) (users map[int64]*User, err error) {
|
|
|
|
if len(usernames) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
users = make(map[int64]*User)
|
|
|
|
err = s.In("username", usernames).Find(&users)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !withEmails {
|
|
|
|
for _, u := range users {
|
|
|
|
u.Email = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:59:31 +02:00
|
|
|
// GetUserWithEmail returns a user object with email
|
2020-12-23 16:32:28 +01:00
|
|
|
func GetUserWithEmail(s *xorm.Session, user *User) (userOut *User, err error) {
|
|
|
|
return getUser(s, user, true)
|
2019-08-14 21:59:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:54:36 +01:00
|
|
|
// GetUsersByIDs returns a map of users from a slice of user ids
|
2020-12-23 16:32:28 +01:00
|
|
|
func GetUsersByIDs(s *xorm.Session, userIDs []int64) (users map[int64]*User, err error) {
|
2020-12-18 14:54:36 +01:00
|
|
|
users = make(map[int64]*User)
|
2021-03-02 18:40:39 +01:00
|
|
|
|
|
|
|
if len(userIDs) == 0 {
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.In("id", userIDs).Find(&users)
|
2020-12-18 14:54:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obfuscate all user emails
|
|
|
|
for _, u := range users {
|
|
|
|
u.Email = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:59:31 +02:00
|
|
|
// getUser is a small helper function to avoid having duplicated code for almost the same use case
|
2020-12-23 16:32:28 +01:00
|
|
|
func getUser(s *xorm.Session, user *User, withEmail bool) (userOut *User, err error) {
|
2019-08-14 21:59:31 +02:00
|
|
|
userOut = &User{} // To prevent a panic if user is nil
|
|
|
|
*userOut = *user
|
2020-12-23 16:32:28 +01:00
|
|
|
exists, err := s.Get(userOut)
|
2020-06-27 19:04:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-10 11:11:41 +02:00
|
|
|
if !exists {
|
2019-08-14 21:59:31 +02:00
|
|
|
return &User{}, ErrUserDoesNotExist{UserID: user.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !withEmail {
|
|
|
|
userOut.Email = ""
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 19:14:02 +02:00
|
|
|
return userOut, err
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
func getUserByUsernameOrEmail(s *xorm.Session, usernameOrEmail string) (u *User, err error) {
|
2020-12-18 14:54:49 +01:00
|
|
|
u = &User{}
|
2020-12-23 16:32:28 +01:00
|
|
|
exists, err := s.
|
2020-12-18 14:54:49 +01:00
|
|
|
Where("username = ? OR email = ?", usernameOrEmail, usernameOrEmail).
|
|
|
|
Get(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return nil, ErrUserDoesNotExist{}
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Email = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// CheckUserCredentials checks user credentials
|
2020-12-23 16:32:28 +01:00
|
|
|
func CheckUserCredentials(s *xorm.Session, u *Login) (*User, error) {
|
2018-11-01 23:51:05 +01:00
|
|
|
// Check if we have any credentials
|
|
|
|
if u.Password == "" || u.Username == "" {
|
2020-12-18 14:54:49 +01:00
|
|
|
return nil, ErrNoUsernamePassword{}
|
2018-11-01 23:51:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// Check if the user exists
|
2020-12-23 16:32:28 +01:00
|
|
|
user, err := getUserByUsernameOrEmail(s, u.Username)
|
2018-06-10 11:11:41 +02:00
|
|
|
if err != nil {
|
2018-12-19 22:05:25 +01:00
|
|
|
// hashing the password takes a long time, so we hash something to not make it clear if the username was wrong
|
2020-04-13 22:30:09 +02:00
|
|
|
_, _ = bcrypt.GenerateFromPassword([]byte(u.Username), 14)
|
2020-12-18 14:54:49 +01:00
|
|
|
return nil, ErrWrongUsernameOrPassword{}
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:54:49 +01:00
|
|
|
// The user is invalid if they need to verify their email address
|
2021-07-13 22:56:02 +02:00
|
|
|
if user.Status == StatusEmailConfirmationRequired {
|
2019-08-14 21:59:31 +02:00
|
|
|
return &User{}, ErrEmailNotConfirmed{UserID: user.ID}
|
2018-11-01 23:47:41 +01:00
|
|
|
}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// Check the users password
|
2020-04-18 01:38:49 +02:00
|
|
|
err = CheckUserPassword(user, u.Password)
|
2018-06-10 11:11:41 +02:00
|
|
|
if err != nil {
|
2021-07-30 15:01:04 +02:00
|
|
|
if IsErrWrongUsernameOrPassword(err) {
|
|
|
|
handleFailedPassword(user)
|
|
|
|
}
|
2020-12-18 14:54:49 +01:00
|
|
|
return nil, err
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 15:01:04 +02:00
|
|
|
func handleFailedPassword(user *User) {
|
|
|
|
key := user.GetFailedPasswordAttemptsKey()
|
|
|
|
err := keyvalue.IncrBy(key, 1)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not set failed password attempts: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a, _, err := keyvalue.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could get failed password attempts for user %d: %s", user.ID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
attempts := a.(int64)
|
|
|
|
if attempts != 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = notifications.Notify(user, &FailedLoginAttemptNotification{
|
|
|
|
User: user,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not send invalid password mail to user: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = keyvalue.Del(key)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not remove failed password attempts: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 01:38:49 +02:00
|
|
|
// CheckUserPassword checks and verifies a user's password. The user object needs to contain the hashed password from the database.
|
|
|
|
func CheckUserPassword(user *User, password string) error {
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
|
|
|
if err != nil {
|
2020-10-11 22:10:03 +02:00
|
|
|
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
|
2020-04-18 01:38:49 +02:00
|
|
|
return ErrWrongUsernameOrPassword{}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 21:08:10 +02:00
|
|
|
// GetCurrentUserFromDB gets a user from jwt claims and returns the full user from the db.
|
|
|
|
func GetCurrentUserFromDB(s *xorm.Session, c echo.Context) (user *User, err error) {
|
|
|
|
u, err := GetCurrentUser(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetUserByID(s, u.ID)
|
|
|
|
}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// GetCurrentUser returns the current user based on its jwt token
|
2018-12-01 00:26:56 +01:00
|
|
|
func GetCurrentUser(c echo.Context) (user *User, err error) {
|
2018-06-10 11:11:41 +02:00
|
|
|
jwtinf := c.Get("user").(*jwt.Token)
|
|
|
|
claims := jwtinf.Claims.(jwt.MapClaims)
|
2019-08-31 22:56:41 +02:00
|
|
|
return GetUserFromClaims(claims)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserFromClaims Returns a new user from jwt claims
|
|
|
|
func GetUserFromClaims(claims jwt.MapClaims) (user *User, err error) {
|
2018-06-10 11:11:41 +02:00
|
|
|
userID, ok := claims["id"].(float64)
|
|
|
|
if !ok {
|
|
|
|
return user, ErrCouldNotGetUserID{}
|
|
|
|
}
|
2018-12-01 00:26:56 +01:00
|
|
|
user = &User{
|
2018-06-10 11:11:41 +02:00
|
|
|
ID: int64(userID),
|
|
|
|
Email: claims["email"].(string),
|
|
|
|
Username: claims["username"].(string),
|
2020-11-21 21:51:55 +01:00
|
|
|
Name: claims["name"].(string),
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2018-12-12 23:50:35 +01:00
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// UpdateUser updates a user
|
2020-12-23 16:32:28 +01:00
|
|
|
func UpdateUser(s *xorm.Session, user *User) (updatedUser *User, err error) {
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
// Check if it exists
|
2020-12-23 16:32:28 +01:00
|
|
|
theUser, err := GetUserWithEmail(s, &User{ID: user.ID})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
2019-08-14 21:59:31 +02:00
|
|
|
return &User{}, err
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have at least a username
|
|
|
|
if user.Username == "" {
|
|
|
|
user.Username = theUser.Username // Dont change the username if we dont have one
|
2020-08-13 17:34:02 +02:00
|
|
|
} else {
|
|
|
|
// Check if the new username already exists
|
2020-12-23 16:32:28 +01:00
|
|
|
uu, err := GetUserByUsername(s, user.Username)
|
2020-08-13 17:34:02 +02:00
|
|
|
if err != nil && !IsErrUserDoesNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if uu.ID != 0 && uu.ID != user.ID {
|
|
|
|
return nil, &ErrUsernameExists{Username: user.Username, UserID: uu.ID}
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 21:51:55 +01:00
|
|
|
// Check if we have a name
|
|
|
|
if user.Name == "" {
|
|
|
|
user.Name = theUser.Name
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:34:02 +02:00
|
|
|
// Check if the email is already used
|
|
|
|
if user.Email == "" {
|
|
|
|
user.Email = theUser.Email
|
|
|
|
} else {
|
2020-12-23 16:32:28 +01:00
|
|
|
uu, err := getUser(s, &User{
|
2020-11-21 17:38:58 +01:00
|
|
|
Email: user.Email,
|
|
|
|
Issuer: user.Issuer,
|
|
|
|
Subject: user.Subject,
|
|
|
|
}, true)
|
2020-08-13 17:34:02 +02:00
|
|
|
if err != nil && !IsErrUserDoesNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if uu.ID != 0 && uu.ID != user.ID {
|
|
|
|
return nil, &ErrUserEmailExists{Email: user.Email, UserID: uu.ID}
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
2020-08-02 19:16:58 +02:00
|
|
|
// Validate the avatar type
|
|
|
|
if user.AvatarProvider != "" {
|
|
|
|
if user.AvatarProvider != "default" &&
|
|
|
|
user.AvatarProvider != "gravatar" &&
|
|
|
|
user.AvatarProvider != "initials" &&
|
2021-12-07 22:11:23 +01:00
|
|
|
user.AvatarProvider != "upload" &&
|
|
|
|
user.AvatarProvider != "marble" {
|
2020-08-02 19:16:58 +02:00
|
|
|
return updatedUser, &ErrInvalidAvatarProvider{AvatarProvider: user.AvatarProvider}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:05:56 +01:00
|
|
|
// Check if we have a valid time zone
|
|
|
|
if user.Timezone == "" {
|
|
|
|
user.Timezone = config.GetTimeZone().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = time.LoadLocation(user.Timezone)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// Update it
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.
|
2020-08-13 17:34:02 +02:00
|
|
|
ID(user.ID).
|
2020-09-27 12:50:52 +02:00
|
|
|
Cols(
|
|
|
|
"username",
|
|
|
|
"email",
|
|
|
|
"avatar_provider",
|
|
|
|
"avatar_file_id",
|
2020-11-21 21:51:55 +01:00
|
|
|
"is_active",
|
|
|
|
"name",
|
2020-12-19 00:21:17 +01:00
|
|
|
"email_reminders_enabled",
|
2021-04-07 18:28:58 +02:00
|
|
|
"discoverable_by_name",
|
|
|
|
"discoverable_by_email",
|
2021-04-11 17:08:43 +02:00
|
|
|
"overdue_tasks_reminders_enabled",
|
2021-06-02 23:20:22 +02:00
|
|
|
"default_list_id",
|
2021-06-03 18:11:44 +02:00
|
|
|
"week_start",
|
2021-12-12 15:39:47 +01:00
|
|
|
"language",
|
2022-01-16 12:05:56 +01:00
|
|
|
"timezone",
|
2020-11-21 21:51:55 +01:00
|
|
|
).
|
2020-08-13 17:34:02 +02:00
|
|
|
Update(user)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
2019-08-14 21:59:31 +02:00
|
|
|
return &User{}, err
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the newly updated user
|
2020-12-23 16:32:28 +01:00
|
|
|
updatedUser, err = GetUserByID(s, user.ID)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
2019-08-14 21:59:31 +02:00
|
|
|
return &User{}, err
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return updatedUser, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUserPassword updates the password of a user
|
2020-12-23 16:32:28 +01:00
|
|
|
func UpdateUserPassword(s *xorm.Session, user *User, newPassword string) (err error) {
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
if newPassword == "" {
|
|
|
|
return ErrEmptyNewPassword{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all user details
|
2020-12-23 16:32:28 +01:00
|
|
|
theUser, err := GetUserByID(s, user.ID)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash the new password and set it
|
2021-04-11 15:17:50 +02:00
|
|
|
hashed, err := HashPassword(newPassword)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
theUser.Password = hashed
|
|
|
|
|
|
|
|
// Update it
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.ID(user.ID).Update(theUser)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-07-29 18:45:22 +02:00
|
|
|
|
|
|
|
// SetStatus sets a users status in the database
|
|
|
|
func (u *User) SetStatus(s *xorm.Session, status Status) (err error) {
|
|
|
|
u.Status = status
|
|
|
|
_, err = s.
|
|
|
|
Where("id = ?", u.ID).
|
|
|
|
Cols("status").
|
|
|
|
Update(u)
|
|
|
|
return
|
|
|
|
}
|