@@ -1347,74 +1347,75 @@ def kerberos(args): # noqa
1347
1347
1348
1348
1349
1349
@cli_utils .action_logging
1350
- def create_user (args ):
1351
- fields = {
1352
- 'role' : args .role ,
1353
- 'username' : args .username ,
1354
- 'email' : args .email ,
1355
- 'firstname' : args .firstname ,
1356
- 'lastname' : args .lastname ,
1357
- }
1358
- empty_fields = [k for k , v in fields .items () if not v ]
1359
- if empty_fields :
1360
- raise SystemExit ('Required arguments are missing: {}.' .format (
1361
- ', ' .join (empty_fields )))
1362
-
1363
- appbuilder = cached_appbuilder ()
1364
- role = appbuilder .sm .find_role (args .role )
1365
- if not role :
1366
- raise SystemExit ('{} is not a valid role.' .format (args .role ))
1367
-
1368
- if args .use_random_password :
1369
- password = '' .join (random .choice (string .printable ) for _ in range (16 ))
1370
- elif args .password :
1371
- password = args .password
1372
- else :
1373
- password = getpass .getpass ('Password:' )
1374
- password_confirmation = getpass .getpass ('Repeat for confirmation:' )
1375
- if password != password_confirmation :
1376
- raise SystemExit ('Passwords did not match!' )
1350
+ def users (args ):
1351
+ if args .list :
1352
+
1353
+ appbuilder = cached_appbuilder ()
1354
+ users = appbuilder .sm .get_all_users ()
1355
+ fields = ['id' , 'username' , 'email' , 'first_name' , 'last_name' , 'roles' ]
1356
+ users = [[user .__getattribute__ (field ) for field in fields ] for user in users ]
1357
+ msg = tabulate (users , [field .capitalize ().replace ('_' , ' ' ) for field in fields ],
1358
+ tablefmt = "fancy_grid" )
1359
+ if sys .version_info [0 ] < 3 :
1360
+ msg = msg .encode ('utf-8' )
1361
+ print (msg )
1377
1362
1378
- if appbuilder .sm .find_user (args .username ):
1379
- print ('{} already exist in the db' .format (args .username ))
1380
1363
return
1381
- user = appbuilder .sm .add_user (args .username , args .firstname , args .lastname ,
1382
- args .email , role , password )
1383
- if user :
1384
- print ('{} user {} created.' .format (args .role , args .username ))
1385
- else :
1386
- raise SystemExit ('Failed to create user.' )
1387
1364
1365
+ if args .create :
1366
+ fields = {
1367
+ 'role' : args .role ,
1368
+ 'username' : args .username ,
1369
+ 'email' : args .email ,
1370
+ 'firstname' : args .firstname ,
1371
+ 'lastname' : args .lastname ,
1372
+ }
1373
+ empty_fields = [k for k , v in fields .items () if not v ]
1374
+ if empty_fields :
1375
+ raise SystemExit ('Required arguments are missing: {}.' .format (
1376
+ ', ' .join (empty_fields )))
1388
1377
1389
- @cli_utils .action_logging
1390
- def delete_user (args ):
1391
- if not args .username :
1392
- raise SystemExit ('Required arguments are missing: username' )
1378
+ appbuilder = cached_appbuilder ()
1379
+ role = appbuilder .sm .find_role (args .role )
1380
+ if not role :
1381
+ raise SystemExit ('{} is not a valid role.' .format (args .role ))
1382
+
1383
+ if args .use_random_password :
1384
+ password = '' .join (random .choice (string .printable ) for _ in range (16 ))
1385
+ elif args .password :
1386
+ password = args .password
1387
+ else :
1388
+ password = getpass .getpass ('Password:' )
1389
+ password_confirmation = getpass .getpass ('Repeat for confirmation:' )
1390
+ if password != password_confirmation :
1391
+ raise SystemExit ('Passwords did not match!' )
1393
1392
1394
- appbuilder = cached_appbuilder ()
1393
+ if appbuilder .sm .find_user (args .username ):
1394
+ print ('{} already exist in the db' .format (args .username ))
1395
+ return
1396
+ user = appbuilder .sm .add_user (args .username , args .firstname , args .lastname ,
1397
+ args .email , role , password )
1398
+ if user :
1399
+ print ('{} user {} created.' .format (args .role , args .username ))
1400
+ else :
1401
+ raise SystemExit ('Failed to create user.' )
1395
1402
1396
- try :
1397
- u = next (u for u in appbuilder .sm .get_all_users () if u .username == args .username )
1398
- except StopIteration :
1399
- raise SystemExit ('{} is not a valid user.' .format (args .username ))
1403
+ if args .delete :
1404
+ if not args .username :
1405
+ raise SystemExit ('Required arguments are missing: username' )
1400
1406
1401
- if appbuilder .sm .del_register_user (u ):
1402
- print ('User {} deleted.' .format (args .username ))
1403
- else :
1404
- raise SystemExit ('Failed to delete user.' )
1407
+ appbuilder = cached_appbuilder ()
1405
1408
1409
+ try :
1410
+ u = next (u for u in appbuilder .sm .get_all_users ()
1411
+ if u .username == args .username )
1412
+ except StopIteration :
1413
+ raise SystemExit ('{} is not a valid user.' .format (args .username ))
1406
1414
1407
- @cli_utils .action_logging
1408
- def list_users (args ):
1409
- appbuilder = cached_appbuilder ()
1410
- users = appbuilder .sm .get_all_users ()
1411
- fields = ['id' , 'username' , 'email' , 'first_name' , 'last_name' , 'roles' ]
1412
- users = [[user .__getattribute__ (field ) for field in fields ] for user in users ]
1413
- msg = tabulate (users , [field .capitalize ().replace ('_' , ' ' ) for field in fields ],
1414
- tablefmt = "fancy_grid" )
1415
- if sys .version_info [0 ] < 3 :
1416
- msg = msg .encode ('utf-8' )
1417
- print (msg )
1415
+ if appbuilder .sm .del_register_user (u ):
1416
+ print ('User {} deleted.' .format (args .username ))
1417
+ else :
1418
+ raise SystemExit ('Failed to delete user.' )
1418
1419
1419
1420
1420
1421
@cli_utils .action_logging
@@ -1527,10 +1528,6 @@ class CLIFactory(object):
1527
1528
"Do not prompt to confirm reset. Use with care!" ,
1528
1529
"store_true" ,
1529
1530
default = False ),
1530
- 'username' : Arg (
1531
- ('-u' , '--username' ,),
1532
- help = 'Username of the user' ,
1533
- type = str ),
1534
1531
1535
1532
# list_dag_runs
1536
1533
'no_backfill' : Arg (
@@ -1886,33 +1883,52 @@ class CLIFactory(object):
1886
1883
('--conn_extra' ,),
1887
1884
help = 'Connection `Extra` field, optional when adding a connection' ,
1888
1885
type = str ),
1889
- # create_user
1890
- 'role' : Arg (
1891
- ('-r' , '--role' ,),
1892
- help = 'Role of the user. Existing roles include Admin, '
1893
- 'User, Op, Viewer, and Public' ,
1886
+ # users
1887
+ 'username' : Arg (
1888
+ ('--username' ,),
1889
+ help = 'Username of the user, required to create/delete a user' ,
1894
1890
type = str ),
1895
1891
'firstname' : Arg (
1896
- ('-f' , '- -firstname' ,),
1897
- help = 'First name of the user' ,
1892
+ ('--firstname' ,),
1893
+ help = 'First name of the user, required to create a user ' ,
1898
1894
type = str ),
1899
1895
'lastname' : Arg (
1900
- ('-l' , '--lastname' ,),
1901
- help = 'Last name of the user' ,
1896
+ ('--lastname' ,),
1897
+ help = 'Last name of the user, required to create a user' ,
1898
+ type = str ),
1899
+ 'role' : Arg (
1900
+ ('--role' ,),
1901
+ help = 'Role of the user. Existing roles include Admin, '
1902
+ 'User, Op, Viewer, and Public. Required to create a user' ,
1902
1903
type = str ),
1903
1904
'email' : Arg (
1904
- ('-e' , '- -email' ,),
1905
- help = 'Email of the user' ,
1905
+ ('--email' ,),
1906
+ help = 'Email of the user, required to create a user ' ,
1906
1907
type = str ),
1907
1908
'password' : Arg (
1908
- ('-p' , '--password' ,),
1909
- help = 'Password of the user' ,
1909
+ ('--password' ,),
1910
+ help = 'Password of the user, required to create a user '
1911
+ 'without --use_random_password' ,
1910
1912
type = str ),
1911
1913
'use_random_password' : Arg (
1912
1914
('--use_random_password' ,),
1913
- help = 'Do not prompt for password. Use random string instead' ,
1915
+ help = 'Do not prompt for password. Use random string instead.'
1916
+ ' Required to create a user without --password ' ,
1914
1917
default = False ,
1915
1918
action = 'store_true' ),
1919
+ 'list_users' : Arg (
1920
+ ('-l' , '--list' ),
1921
+ help = 'List all users' ,
1922
+ action = 'store_true' ),
1923
+ 'create_user' : Arg (
1924
+ ('-c' , '--create' ),
1925
+ help = 'Create a user' ,
1926
+ action = 'store_true' ),
1927
+ 'delete_user' : Arg (
1928
+ ('-d' , '--delete' ),
1929
+ help = 'Delete a user' ,
1930
+ action = 'store_true' ),
1931
+
1916
1932
}
1917
1933
subparsers = (
1918
1934
{
@@ -2070,18 +2086,11 @@ class CLIFactory(object):
2070
2086
'args' : ('list_connections' , 'add_connection' , 'delete_connection' ,
2071
2087
'conn_id' , 'conn_uri' , 'conn_extra' ) + tuple (alternative_conn_specs ),
2072
2088
}, {
2073
- 'func' : create_user ,
2074
- 'help' : "Create an account for the Web UI (FAB-based)" ,
2075
- 'args' : ('role' , 'username' , 'email' , 'firstname' , 'lastname' ,
2089
+ 'func' : users ,
2090
+ 'help' : "List/Create/Delete users" ,
2091
+ 'args' : ('list_users' , 'create_user' , 'delete_user' ,
2092
+ 'username' , 'email' , 'firstname' , 'lastname' , 'role' ,
2076
2093
'password' , 'use_random_password' ),
2077
- }, {
2078
- 'func' : delete_user ,
2079
- 'help' : "Delete an account for the Web UI" ,
2080
- 'args' : ('username' ,),
2081
- }, {
2082
- 'func' : list_users ,
2083
- 'help' : "List accounts for the Web UI" ,
2084
- 'args' : tuple (),
2085
2094
},
2086
2095
{
2087
2096
'func' : sync_perm ,
0 commit comments