2019-01-08 08:57:24 +00:00
package cmd
import (
"errors"
"os"
"strconv"
"github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra"
)
func init ( ) {
usersCmd . AddCommand ( usersImportCmd )
usersImportCmd . Flags ( ) . Bool ( "overwrite" , false , "overwrite users with the same id/username combo" )
2019-01-08 12:02:49 +00:00
usersImportCmd . Flags ( ) . Bool ( "replace" , false , "replace the entire user base" )
2019-01-08 08:57:24 +00:00
}
var usersImportCmd = & cobra . Command {
2019-01-08 16:37:02 +00:00
Use : "import <path>" ,
2019-01-08 16:09:25 +00:00
Short : "Import users from a file" ,
2019-01-08 16:37:02 +00:00
Long : ` Import users from a file . The path must be for a json or yaml
file . You can use this command to import new users to your
installation . For that , just don ' t place their ID on the files
list or set it to 0. ` ,
Args : jsonYamlArg ,
2019-01-08 08:57:24 +00:00
Run : python ( func ( cmd * cobra . Command , args [ ] string , d pythonData ) {
fd , err := os . Open ( args [ 0 ] )
checkErr ( err )
defer fd . Close ( )
list := [ ] * users . User { }
err = unmarshal ( args [ 0 ] , & list )
checkErr ( err )
for _ , user := range list {
err = user . Clean ( "" )
checkErr ( err )
}
2019-01-08 14:07:55 +00:00
if mustGetBool ( cmd . Flags ( ) , "replace" ) {
2019-01-08 12:02:49 +00:00
oldUsers , err := d . store . Users . Gets ( "" )
checkErr ( err )
2019-01-08 16:09:25 +00:00
2019-01-08 12:02:49 +00:00
err = marshal ( "users.backup.json" , list )
checkErr ( err )
2019-01-08 16:09:25 +00:00
2019-01-08 12:02:49 +00:00
for _ , user := range oldUsers {
err = d . store . Users . Delete ( user . ID )
checkErr ( err )
}
}
2019-01-08 16:09:25 +00:00
2019-01-08 14:07:55 +00:00
overwrite := mustGetBool ( cmd . Flags ( ) , "overwrite" )
2019-01-08 08:57:24 +00:00
for _ , user := range list {
2019-01-08 12:02:49 +00:00
onDB , err := d . store . Users . Get ( "" , user . ID )
2019-01-08 08:57:24 +00:00
// User exists in DB.
if err == nil {
if ! overwrite {
checkErr ( errors . New ( "user " + strconv . Itoa ( int ( user . ID ) ) + " is already registred" ) )
}
// If the usernames mismatch, check if there is another one in the DB
// with the new username. If there is, print an error and cancel the
// operation
2019-01-08 12:02:49 +00:00
if user . Username != onDB . Username {
2019-01-08 08:57:24 +00:00
conflictuous , err := d . store . Users . Get ( "" , user . Username )
if err == nil {
checkErr ( usernameConflictError ( user . Username , conflictuous . ID , user . ID ) )
}
}
2019-01-26 11:37:33 +00:00
} else {
// If it doesn't exist, set the ID to 0 to automatically get a new
// one that make sense in this DB.
user . ID = 0
2019-01-08 08:57:24 +00:00
}
err = d . store . Users . Save ( user )
checkErr ( err )
}
} , pythonConfig { } ) ,
}
func usernameConflictError ( username string , original , new uint ) error {
return errors . New ( "can't import user with ID " + strconv . Itoa ( int ( new ) ) + " and username \"" + username + "\" because the username is already registred with the user " + strconv . Itoa ( int ( original ) ) )
}