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.
2020-08-13 17:34:02 +02: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
2020-08-13 17:34:02 +02:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2020-12-29 02:04:20 +01:00
// This program is distributed in the hope that it will be useful,
2020-08-13 17:34:02 +02: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.
2020-08-13 17:34:02 +02: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/>.
2020-08-13 17:34:02 +02:00
package cmd
import (
2021-08-11 21:08:10 +02:00
"bufio"
2020-10-11 22:10:03 +02:00
"fmt"
"os"
"strconv"
"strings"
"time"
2022-03-04 19:58:40 +01:00
"github.com/asaskevich/govalidator"
2020-12-23 16:32:28 +01:00
"code.vikunja.io/api/pkg/db"
2020-08-13 17:34:02 +02:00
"code.vikunja.io/api/pkg/initialize"
"code.vikunja.io/api/pkg/log"
2020-11-21 17:38:58 +01:00
"code.vikunja.io/api/pkg/models"
2020-08-13 17:34:02 +02:00
"code.vikunja.io/api/pkg/user"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
2020-12-02 22:33:03 +01:00
"golang.org/x/term"
2020-12-23 16:32:28 +01:00
"xorm.io/xorm"
2020-08-13 17:34:02 +02:00
)
var (
userFlagUsername string
userFlagEmail string
userFlagPassword string
userFlagAvatar = "default"
userFlagResetPasswordDirectly bool
userFlagEnableUser bool
userFlagDisableUser bool
2021-08-11 21:08:10 +02:00
userFlagDeleteNow bool
2020-08-13 17:34:02 +02:00
)
func init ( ) {
// User create flags
userCreateCmd . Flags ( ) . StringVarP ( & userFlagUsername , "username" , "u" , "" , "The username of the new user." )
_ = userCreateCmd . MarkFlagRequired ( "username" )
userCreateCmd . Flags ( ) . StringVarP ( & userFlagEmail , "email" , "e" , "" , "The email address of the new user." )
_ = userCreateCmd . MarkFlagRequired ( "email" )
userCreateCmd . Flags ( ) . StringVarP ( & userFlagPassword , "password" , "p" , "" , "The password of the new user. You will be asked to enter it if not provided through the flag." )
userCreateCmd . Flags ( ) . StringVarP ( & userFlagAvatar , "avatar-provider" , "a" , "" , "The avatar provider of the new user. Optional." )
// User update flags
userUpdateCmd . Flags ( ) . StringVarP ( & userFlagUsername , "username" , "u" , "" , "The new username of the user." )
userUpdateCmd . Flags ( ) . StringVarP ( & userFlagEmail , "email" , "e" , "" , "The new email address of the user." )
userUpdateCmd . Flags ( ) . StringVarP ( & userFlagAvatar , "avatar-provider" , "a" , "" , "The new avatar provider of the new user." )
// Reset PW flags
userResetPasswordCmd . Flags ( ) . BoolVarP ( & userFlagResetPasswordDirectly , "direct" , "d" , false , "If provided, reset the password directly instead of sending the user a reset mail." )
userResetPasswordCmd . Flags ( ) . StringVarP ( & userFlagPassword , "password" , "p" , "" , "The new password of the user. Only used in combination with --direct. You will be asked to enter it if not provided through the flag." )
// Change status flags
userChangeEnabledCmd . Flags ( ) . BoolVarP ( & userFlagDisableUser , "disable" , "d" , false , "Disable the user." )
userChangeEnabledCmd . Flags ( ) . BoolVarP ( & userFlagEnableUser , "enable" , "e" , false , "Enable the user." )
2021-08-11 21:08:10 +02:00
// User deletion flags
userDeleteCmd . Flags ( ) . BoolVarP ( & userFlagDeleteNow , "now" , "n" , false , "If provided, deletes the user immediately instead of sending them an email first." )
userCmd . AddCommand ( userListCmd , userCreateCmd , userUpdateCmd , userResetPasswordCmd , userChangeEnabledCmd , userDeleteCmd )
2020-08-13 17:34:02 +02:00
rootCmd . AddCommand ( userCmd )
}
func getPasswordFromFlagOrInput ( ) ( pw string ) {
pw = userFlagPassword
if userFlagPassword == "" {
fmt . Print ( "Enter Password: " )
2020-12-02 22:33:03 +01:00
bytePW , err := term . ReadPassword ( int ( os . Stdin . Fd ( ) ) )
2020-08-13 17:34:02 +02:00
if err != nil {
log . Fatalf ( "Error reading password: %s" , err )
}
fmt . Printf ( "\nConfirm Password: " )
2020-12-02 22:33:03 +01:00
byteConfirmPW , err := term . ReadPassword ( int ( os . Stdin . Fd ( ) ) )
2020-08-13 17:34:02 +02:00
if err != nil {
log . Fatalf ( "Error reading password: %s" , err )
}
if string ( bytePW ) != string ( byteConfirmPW ) {
log . Critical ( "Passwords don't match!" )
}
fmt . Printf ( "\n" )
pw = strings . TrimSpace ( string ( bytePW ) )
}
return
}
2020-12-23 16:32:28 +01:00
func getUserFromArg ( s * xorm . Session , arg string ) * user . User {
2020-08-13 17:34:02 +02:00
id , err := strconv . ParseInt ( arg , 10 , 64 )
if err != nil {
log . Fatalf ( "Invalid user id: %s" , err )
}
2021-10-16 17:00:48 +02:00
u , err := user . GetUserWithEmail ( s , & user . User { ID : id } )
2020-08-13 17:34:02 +02:00
if err != nil {
log . Fatalf ( "Could not get user: %s" , err )
}
return u
}
var userCmd = & cobra . Command {
Use : "user" ,
Short : "Manage users locally through the cli." ,
}
var userListCmd = & cobra . Command {
Use : "list" ,
Short : "Shows a list of all users." ,
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
initialize . FullInit ( )
} ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2020-12-23 16:32:28 +01:00
s := db . NewSession ( )
defer s . Close ( )
2021-04-07 18:28:58 +02:00
users , err := user . ListAllUsers ( s )
2020-08-13 17:34:02 +02:00
if err != nil {
2020-12-23 16:32:28 +01:00
_ = s . Rollback ( )
log . Fatalf ( "Error getting users: %s" , err )
}
if err := s . Commit ( ) ; err != nil {
2020-08-13 17:34:02 +02:00
log . Fatalf ( "Error getting users: %s" , err )
}
table := tablewriter . NewWriter ( os . Stdout )
table . SetHeader ( [ ] string {
"ID" ,
"Username" ,
"Email" ,
2021-07-13 22:56:02 +02:00
"Status" ,
2020-08-13 17:34:02 +02:00
"Created" ,
"Updated" ,
} )
for _ , u := range users {
table . Append ( [ ] string {
strconv . FormatInt ( u . ID , 10 ) ,
u . Username ,
u . Email ,
2021-07-13 22:56:02 +02:00
u . Status . String ( ) ,
2020-08-13 17:34:02 +02:00
u . Created . Format ( time . RFC3339 ) ,
u . Updated . Format ( time . RFC3339 ) ,
} )
}
table . Render ( )
} ,
}
var userCreateCmd = & cobra . Command {
Use : "create" ,
Short : "Create a new user." ,
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
initialize . FullInit ( )
} ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2020-12-23 16:32:28 +01:00
s := db . NewSession ( )
defer s . Close ( )
2020-08-13 17:34:02 +02:00
u := & user . User {
Username : userFlagUsername ,
Email : userFlagEmail ,
Password : getPasswordFromFlagOrInput ( ) ,
}
2022-03-04 19:58:40 +01:00
if ! govalidator . IsEmail ( userFlagEmail ) {
log . Fatalf ( "Provided email is invalid." )
}
2020-12-23 16:32:28 +01:00
newUser , err := user . CreateUser ( s , u )
2020-08-13 17:34:02 +02:00
if err != nil {
2020-12-23 16:32:28 +01:00
_ = s . Rollback ( )
2020-08-13 17:34:02 +02:00
log . Fatalf ( "Error creating new user: %s" , err )
}
2020-12-23 16:32:28 +01:00
err = models . CreateNewNamespaceForUser ( s , newUser )
2020-11-21 17:38:58 +01:00
if err != nil {
2020-12-23 16:32:28 +01:00
_ = s . Rollback ( )
2020-11-21 17:38:58 +01:00
log . Fatalf ( "Error creating new namespace for user: %s" , err )
}
2020-12-23 16:32:28 +01:00
if err := s . Commit ( ) ; err != nil {
log . Fatalf ( "Error saving everything: %s" , err )
}
2020-08-13 17:34:02 +02:00
fmt . Printf ( "\nUser was created successfully.\n" )
} ,
}
var userUpdateCmd = & cobra . Command {
Use : "update [user id]" ,
Short : "Update an existing user." ,
Args : cobra . ExactArgs ( 1 ) ,
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
initialize . FullInit ( )
} ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2020-12-23 16:32:28 +01:00
s := db . NewSession ( )
defer s . Close ( )
u := getUserFromArg ( s , args [ 0 ] )
2020-08-13 17:34:02 +02:00
if userFlagUsername != "" {
u . Username = userFlagUsername
}
if userFlagEmail != "" {
u . Email = userFlagEmail
}
if userFlagAvatar != "default" {
u . AvatarProvider = userFlagAvatar
}
2020-12-23 16:32:28 +01:00
_ , err := user . UpdateUser ( s , u )
2020-08-13 17:34:02 +02:00
if err != nil {
2020-12-23 16:32:28 +01:00
_ = s . Rollback ( )
2020-08-13 17:34:02 +02:00
log . Fatalf ( "Error updating the user: %s" , err )
}
2020-12-23 16:32:28 +01:00
if err := s . Commit ( ) ; err != nil {
log . Fatalf ( "Error saving everything: %s" , err )
}
2020-08-13 17:34:02 +02:00
fmt . Println ( "User updated successfully." )
} ,
}
var userResetPasswordCmd = & cobra . Command {
Use : "reset-password [user id]" ,
Short : "Reset a users password, either through mailing them a reset link or directly." ,
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
initialize . FullInit ( )
} ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2020-12-23 16:32:28 +01:00
s := db . NewSession ( )
defer s . Close ( )
u := getUserFromArg ( s , args [ 0 ] )
2020-08-13 17:34:02 +02:00
// By default we reset as usual, only with specific flag directly.
if userFlagResetPasswordDirectly {
2020-12-23 16:32:28 +01:00
err := user . UpdateUserPassword ( s , u , getPasswordFromFlagOrInput ( ) )
2020-08-13 17:34:02 +02:00
if err != nil {
2020-12-23 16:32:28 +01:00
_ = s . Rollback ( )
2020-08-13 17:34:02 +02:00
log . Fatalf ( "Could not update user password: %s" , err )
}
fmt . Println ( "Password updated successfully." )
} else {
2020-12-23 16:32:28 +01:00
err := user . RequestUserPasswordResetToken ( s , u )
2020-08-13 17:34:02 +02:00
if err != nil {
2020-12-23 16:32:28 +01:00
_ = s . Rollback ( )
2020-08-13 17:34:02 +02:00
log . Fatalf ( "Could not send password reset email: %s" , err )
}
fmt . Println ( "Password reset email sent successfully." )
}
2020-12-23 16:32:28 +01:00
if err := s . Commit ( ) ; err != nil {
log . Fatalf ( "Could not send password reset email: %s" , err )
}
2020-08-13 17:34:02 +02:00
} ,
}
var userChangeEnabledCmd = & cobra . Command {
Use : "change-status [user id]" ,
Short : "Enable or disable a user. Will toggle the current status if no flag (--enable or --disable) is provided." ,
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
initialize . FullInit ( )
} ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2020-12-23 16:32:28 +01:00
s := db . NewSession ( )
defer s . Close ( )
u := getUserFromArg ( s , args [ 0 ] )
2020-08-13 17:34:02 +02:00
if userFlagEnableUser {
2021-07-13 22:56:02 +02:00
u . Status = user . StatusActive
2020-08-13 17:34:02 +02:00
} else if userFlagDisableUser {
2021-07-13 22:56:02 +02:00
u . Status = user . StatusDisabled
2020-08-13 17:34:02 +02:00
} else {
2021-07-13 22:56:02 +02:00
if u . Status == user . StatusActive {
u . Status = user . StatusDisabled
} else {
u . Status = user . StatusActive
}
2020-08-13 17:34:02 +02:00
}
2020-12-23 16:32:28 +01:00
_ , err := user . UpdateUser ( s , u )
2020-08-13 17:34:02 +02:00
if err != nil {
2020-12-23 16:32:28 +01:00
_ = s . Rollback ( )
2020-08-13 17:34:02 +02:00
log . Fatalf ( "Could not enable the user" )
}
2020-12-23 16:32:28 +01:00
if err := s . Commit ( ) ; err != nil {
log . Fatalf ( "Error saving everything: %s" , err )
}
2021-07-13 22:56:02 +02:00
fmt . Printf ( "User status successfully changed, status is now \"%s\"\n" , u . Status )
2020-08-13 17:34:02 +02:00
} ,
}
2021-08-11 21:08:10 +02:00
var userDeleteCmd = & cobra . Command {
Use : "delete [user id]" ,
Short : "Delete an existing user." ,
Long : "Kick off the user deletion process. If call without the --now flag, this command will only trigger an email to the user in order for them to confirm and start the deletion process. With the flag the user is deleted immediately. USE WITH CAUTION." ,
Args : cobra . ExactArgs ( 1 ) ,
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
initialize . FullInit ( )
} ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
if userFlagDeleteNow {
fmt . Println ( "You requested to delete the user immediately. Are you sure?" )
fmt . Println ( ` To confirm, please type "yes, I confirm" in all uppercase: ` )
cr := bufio . NewReader ( os . Stdin )
text , err := cr . ReadString ( '\n' )
if err != nil {
log . Fatalf ( "could not read confirmation message: %s" , err )
}
if text != "YES, I CONFIRM\n" {
log . Fatalf ( "invalid confirmation message" )
}
}
s := db . NewSession ( )
defer s . Close ( )
if err := s . Begin ( ) ; err != nil {
log . Fatalf ( "Count not start transaction: %s" , err )
}
u := getUserFromArg ( s , args [ 0 ] )
if userFlagDeleteNow {
err := models . DeleteUser ( s , u )
if err != nil {
_ = s . Rollback ( )
log . Fatalf ( "Error removing the user: %s" , err )
}
} else {
err := user . RequestDeletion ( s , u )
if err != nil {
_ = s . Rollback ( )
log . Fatalf ( "Could not request user deletion: %s" , err )
}
}
if err := s . Commit ( ) ; err != nil {
log . Fatalf ( "Error saving everything: %s" , err )
}
if userFlagDeleteNow {
fmt . Println ( "User deleted successfully." )
} else {
fmt . Println ( "User scheduled for deletion successfully." )
}
} ,
}