def handle_accounts(): if request.method == 'GET': # Retrieve accounts list accounts = AccountManager().get_accounts() accs = [] for account in accounts: state = get_state(account) state['id'] = account.id if 'auth' in state: state['auth']['password'] = '******' accs.append(state) return jsonify({'accounts': accs}) elif request.method == 'POST': # Create account state = get_json(request) if not state: return error_response(400, 'error processing POST body') account_id = state.pop('id', None) if not account_id: return error_response(400, 'account ID was not specified') try: account = Account(account_id) except DuplicateIDError: return error_response(409, 'duplicated account ID') try: set_state(account, state) except ValueError, e: account.delete() return error_response(400, str(e)) account.enabled = True account.save() state = get_state(account) if 'auth' in state: state['auth']['password'] = '******' return jsonify({'account': state}), 201
def updateAccountFromCloud(self, key): def set_state(obj, state): for name, value in state.iteritems(): attribute = getattr(obj.__class__, name, None) if isinstance(attribute, SettingsGroupMeta): group = getattr(obj, name) set_state(group, value) elif isinstance(attribute, Setting): if issubclass(attribute.type, bool) and isinstance(value, bool): value = str(value) try: attribute.__setstate__(obj, value) except ValueError: pass else: attribute.dirty[obj] = True json_data = self.cloud_storage.stringForKey_(key) am = AccountManager() if json_data: try: data = cjson.decode(json_data) except TypeError: # account has been deleted in the mean time try: account = am.get_account(key) if isinstance(account, Account): account.delete() except KeyError: pass try: account = am.get_account(key) except KeyError: account = Account(key) set_state(account, data) account.save() # update keychain passwords if isinstance(account, Account): passwords = {'auth': {'label': '%s (%s)' % (NSApp.delegate().applicationName, account.id), 'value': account.auth.password}, 'web': {'label': '%s WEB (%s)' % (NSApp.delegate().applicationName, account.id), 'value': account.server.web_password}, 'ldap': {'label': '%s LDAP (%s)' % (NSApp.delegate().applicationName, account.id), 'value': account.ldap.password}, 'chat': {'label': '%s ChatReplication (%s)' % (NSApp.delegate().applicationName, account.id), 'value': account.chat.replication_password} } for p in passwords.keys(): label = passwords[p]['label'] value = passwords[p]['value'] k = EMGenericKeychainItem.genericKeychainItemForService_withUsername_(label, account.id) if k is None and value: EMGenericKeychainItem.addGenericKeychainItemForService_withUsername_password_(label, account.id, value) elif k is not None: if value: k.setPassword_(value) else: k.removeFromKeychain() else: try: account = am.get_account(key) if isinstance(account, Account): account.delete() except KeyError: pass self.notification_center.post_notification("SIPAccountChangedByICloud", sender=self, data=TimestampedNotificationData(account=key))