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

Method execute

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

Source from the content-addressed store, hash-verified

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

Calls 14

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