MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / execute

Method execute

keepercommander/commands/register.py:1285–1352  ·  view source on GitHub ↗
(self, params, **kwargs)

Source from the content-addressed store, hash-verified

1283
1284
1285 def execute(self, params, **kwargs):
1286 name = kwargs.get('record')
1287 if not name:
1288 self.get_parser().print_help()
1289 return
1290
1291 emails = kwargs.get('email') or []
1292 if not emails:
1293 raise CommandError('share-record', '\'email\' parameter is missing')
1294
1295 from ..enforcement import MasterPasswordReentryEnforcer
1296 if not MasterPasswordReentryEnforcer.check_and_enforce(params, "record_level"):
1297 raise CommandError('share-record', 'Operation cancelled: Re-authentication failed')
1298
1299 force = kwargs.get('force') is True
1300 action = kwargs.get('action') or 'grant'
1301 use_contacts = kwargs.get('contacts_only')
1302
1303 def get_contact(user, contacts):
1304 get_username = lambda addr: next(iter(addr.split('@')), '').casefold()
1305 matches = [c for c in contacts if get_username(user) == get_username(c)]
1306 if len(matches) > 1:
1307 raise CommandError('share-record', 'More than 1 matching usernames found. Aborting')
1308 return next(iter(matches), None)
1309
1310 if use_contacts:
1311 known_users = api.get_share_objects(params).get('users', {})
1312 known_emails = [u.casefold() for u in known_users.keys()]
1313 is_unknown = lambda e: e.casefold() not in known_emails and is_email(e)
1314 unknowns = [e for e in emails if is_unknown(e)]
1315 if unknowns:
1316 username_map = {e: get_contact(e, known_users) for e in unknowns}
1317 table = [[k, v] for k, v in username_map.items()]
1318 logging.info(f'{len(unknowns)} unrecognized share recipient(s) and closest matching contact(s)')
1319 dump_report_data(table, ['Username', 'From Contacts'])
1320 confirmed = force or user_choice('\tReplace with known matching contact(s)?', 'yn', default='n') == 'y'
1321 if confirmed:
1322 good_emails = [e for e in emails if e not in unknowns]
1323 replacements = [e for e in username_map.values() if e]
1324 emails = [*good_emails, *replacements]
1325
1326 if action == 'cancel':
1327 if not force:
1328 answer = base.user_choice(
1329 bcolors.FAIL + bcolors.BOLD + '\nALERT!\n' + bcolors.ENDC + 'This action cannot be undone.\n\n' +
1330 'Do you want to cancel all shares with user(s): ' + ', '.join(emails) + ' ?', 'yn', 'n')
1331 else:
1332 answer = 'y'
1333 if answer.lower() in {'y', 'yes'}:
1334 for email in emails:
1335 rq = {
1336 'command': 'cancel_share',
1337 'to_email': email
1338 }
1339 try:
1340 api.communicate(params, rq)
1341 except KeeperApiError as kae:
1342 if kae.result_code == 'share_not_found':

Calls 13

get_parserMethod · 0.95
is_emailFunction · 0.85
dump_report_dataFunction · 0.85
user_choiceFunction · 0.85
check_and_enforceMethod · 0.80
formatMethod · 0.80
warningMethod · 0.80
prep_requestMethod · 0.80
CommandErrorClass · 0.70
getMethod · 0.45
print_helpMethod · 0.45
infoMethod · 0.45