Esempio n. 1
0
 def _send_single(self, action, **payload):
     result = self.container.send_action(action, payload)
     if 'error' in result:
         raise SkygearException.from_dict(result['error'])
     elif 'result' in result and isinstance(result['result'], list) \
             and len(result['result']) > 0:
         first_result = result['result'][0]
         if first_result.get('_type', None) == 'error':
             raise SkygearException.from_dict(first_result)
         return first_result
     else:
         raise SkygearException('unexpected result', UnexpectedError)
Esempio n. 2
0
 def _send_single(self, action, **payload):
     result = self.container.send_action(action, payload)
     if 'error' in result:
         raise SkygearException.from_dict(result['error'])
     elif 'result' in result and isinstance(result['result'], list) \
             and len(result['result']) > 0:
         first_result = result['result'][0]
         if first_result.get('_type', None) == 'error':
             raise SkygearException.from_dict(first_result)
         return first_result
     else:
         raise SkygearException('unexpected result', UnexpectedError)
Esempio n. 3
0
 def _send_multi(self, action, **payload):
     result = self.container.send_action(action, payload)
     if 'error' in result:
         raise SkygearException.from_dict(result['error'])
     elif 'result' in result and isinstance(result['result'], list):
         return result
     else:
         raise SkygearException('unexpected result', UnexpectedError)
Esempio n. 4
0
 def _send_multi(self, action, **payload):
     result = self.container.send_action(action, payload)
     if 'error' in result:
         raise SkygearException.from_dict(result['error'])
     elif 'result' in result and isinstance(result['result'], list):
         return result
     else:
         raise SkygearException('unexpected result', UnexpectedError)
Esempio n. 5
0
def schema_add_key_verified_acl(flag_names):
    """
    Add field ACL to disallow owner from modifying verified flags.
    """
    container = SkygearContainer(api_key=skyoptions.masterkey)

    # Fetch the current field ACL. If no changes are required, we will
    # not try to update the field ACL.
    resp = container.send_action("schema:field_access:get", {},
                                 plugin_request=True)
    if "error" in resp:
        raise SkygearException.from_dict(resp["error"])

    # Create the new ACL settings. This is accomplished by first
    # copying the existing field ACLs, ignoring the entries we need
    # to enforce.
    new_acls = []
    for acl in resp['result']['access']:
        if acl['record_type'] == 'user' \
                and acl['record_field'] in flag_names \
                and acl['user_role'] == '_owner':
            continue
        new_acls.append(acl)

    for flag_name in flag_names:
        new_acls.append({
            'record_type': 'user',
            'record_field': flag_name,
            'user_role': '_owner',
            'writable': False,
            'readable': True,
            'comparable': True,
            'discoverable': True,
        })

    if not new_acls:
        return

    # Update the field ACL.
    resp = container.send_action("schema:field_access:update",
                                 {"access": new_acls},
                                 plugin_request=True)
    if "error" in resp:
        raise SkygearException.from_dict(resp["error"])
Esempio n. 6
0
def schema_add_key_verified_flags(flag_names):
    """
    Add the fields into Skygear DB user record.

    This function adds the specified fields into the user record schema. It
    is expected that all fields have boolean data type.
    """
    if not flag_names:
        return

    container = SkygearContainer(api_key=skyoptions.masterkey)

    # Fetch schema first. If no changes are required, we will not try
    # to update the schema.
    resp = container.send_action("schema:fetch", {}, plugin_request=True)
    if "error" in resp:
        raise SkygearException.from_dict(resp["error"])

    for field in resp['result']['record_types']['user']['fields']:
        if field['name'] in flag_names:
            flag_names.remove(field['name'])

    # `flag_names` now contain the list of fields to create. Proceed
    # to create the field list and save the new fields to user record
    # schema.
    fields = [{
        'name': flag_name,
        'type': 'boolean'
    } for flag_name in flag_names]

    resp = container.send_action(
        "schema:create", {"record_types": {
            'user': {
                'fields': fields
            }
        }},
        plugin_request=True)
    if "error" in resp:
        raise SkygearException.from_dict(resp["error"])
Esempio n. 7
0
def save_user_record(user_record):
    """
    Save the user record to Skygear Record API.
    """
    container = SkygearContainer(api_key=skyoptions.masterkey)

    resp = container.send_action("record:save",
                                 {"records": [serialize_record(user_record)]},
                                 plugin_request=True)
    try:
        if "error" in resp:
            raise SkygearException.from_dict(resp["error"])
    except (ValueError, TypeError, KeyError):
        raise SkygearContainer("container.send_action is buggy")
Esempio n. 8
0
def fetch_user_record(auth_id):
    """
    Fetch the user record from Skygear Record API. The returned value
    is a user record in Record class.
    """
    container = SkygearContainer(api_key=skyoptions.masterkey)

    resp = container.send_action("record:fetch",
                                 {"ids": ['user/{}'.format(auth_id)]},
                                 plugin_request=True)
    try:
        if "error" in resp:
            raise SkygearException.from_dict(resp["error"])
    except (ValueError, TypeError, KeyError):
        raise SkygearContainer("container.send_action is buggy")
    return deserialize_record(resp['result'][0])
Esempio n. 9
0
def set_new_password(user_id, new_password):
    """
    Set the password of a user to a new password
    with auth:reset_password
    """
    container = SkygearContainer(api_key=skyoptions.masterkey)
    resp = container.send_action("auth:reset_password", {
        "auth_id": user_id,
        "password": new_password,
    },
                                 plugin_request=True)
    try:
        if "error" in resp:
            raise SkygearException.from_dict(resp["error"])
    except (ValueError, TypeError, KeyError):
        raise SkygearContainer("container.send_action is buggy")