diff --git a/cli/add-user b/cli/add-user new file mode 120000 index 0000000..1646e46 --- /dev/null +++ b/cli/add-user @@ -0,0 +1 @@ +../scripts/add-user.php \ No newline at end of file diff --git a/scripts/add-user.php b/scripts/add-user.php new file mode 100755 index 0000000..7933f11 --- /dev/null +++ b/scripts/add-user.php @@ -0,0 +1,126 @@ +#!/usr/bin/php +. + +// allowed only from command line interface +if( ! isset( $argv[ 0 ] ) ) { + exit( 1 ); +} + +// autoload the framework +require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'load.php'; + +// command line arguments +$opts = getopt( 'h', [ + 'uid:', + 'role:', + 'email:', + 'name:', + 'surname:', + 'pwd:', + 'force::', + 'help', +] ); + +// show help +if( ! isset( $opts[ 'uid' ], $opts[ 'pwd' ], $opts[ 'role' ] ) || isset( $opts[ 'help' ] ) || isset( $opts[ 'h' ] ) ) { + + $roles = _roles(); + $roles_list = implode( '|', $roles ); + + printf( "Usage: %s [OPTIONS]\n", $argv[ 0 ] ); + echo "OPTIONS:\n"; + echo " --uid=UID username\n"; + echo " --email=EMAIL email\n"; + echo " --name=NAME first name\n"; + echo " --surname=SURNAME family name\n"; + echo " --role=ROLE role ($roles_list)\n"; + echo " --pwd=PASSWORD password\n"; + echo " --force update the user password if exists\n"; + echo " -h --help show this help and exit\n"; + exit( 0 ); +} + +// validate role +if( !Permissions::instance()->roleExists( $opts['role'] ) ) { + printf( "The role '%s' does not exist\n", $opts['role'] ); + exit( 1 ); +} + +// look for existing user +$user = User::factoryFromUID( $opts[ 'uid' ] ) + ->select( User::ID ) + ->queryRow(); + +// check if it exists +if( $user && ! isset( $opts[ 'force' ] ) ) { + printf( "User %s already exist\n", $opts[ 'uid' ] ); + exit( 1 ); +} + +// encrypt the password +$pwd = User::encryptPassword( $opts[ 'pwd' ] ); + +if( $user ) { + + // update the User + ( new UserAPI() ) + ->whereUser( $user ) + ->update( [ + 'user_password' => $pwd, + ] ); + + echo "Updated.\n"; + +} else { + + // insert a new user + ( new UserAPI() ) + ->insertRow( [ + 'user_uid' => $opts[ 'uid' ], + 'user_role' => $opts[ 'role' ], + 'user_name' => $opts[ 'name' ], + 'user_surname' => $opts[ 'surname' ], + 'user_email' => $opts[ 'email' ], + 'user_password' => $pwd, + 'user_active' => 1, + ] ); + + echo "Created\n"; +} + + +/** + * Get a list of available roles + * + * Well, it just remove the DEFAULT_USER_ROLE from the roles. + * + * @return array + */ +function _roles() { + + $good_roles = []; + + // get the existing roles + foreach( Permissions::instance()->getRoles() as $role ) { + if( $role !== DEFAULT_USER_ROLE ) { + $good_roles[] = $role; + } + } + + return $good_roles; +}