Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Commit

Permalink
Added role and ACL support
Browse files Browse the repository at this point in the history
  • Loading branch information
dankrause committed Nov 7, 2014
1 parent 1f23538 commit 2aad3cd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ parse_rest
- Cloud code integration
- Installation querying
- push
- **PLANNED/TODO**: Roles/ACLs**
- Roles/ACLs**
- **PLANNED/TODO**: Image/File type support


Expand Down
65 changes: 59 additions & 6 deletions parse_rest/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ class ParseType(object):
type_mapping = {}

@staticmethod
def convert_from_parse(parse_data):
def convert_from_parse(parse_key, parse_data):

is_parse_type = isinstance(parse_data, dict) and '__type' in parse_data
parse_type = None
if isinstance(parse_data, dict):
if '__type' in parse_data:
parse_type = parse_data.pop('__type')
elif parse_key == 'ACL':
parse_type = 'ACL'

# if its not a parse type -- simply return it. This means it wasn't a "special class"
if not is_parse_type:
if not parse_type:
return parse_data

native = ParseType.type_mapping.get(parse_data.pop('__type'))
native = ParseType.type_mapping.get(parse_type)
return native.from_native(**parse_data) if native else parse_data

@staticmethod
Expand All @@ -60,7 +65,7 @@ def convert_to_parse(python_object, as_pointer=False):
ParseResource: Pointer
}

if hasattr(python_object, '__iter__') and not isinstance(python_object, str):
if hasattr(python_object, '__iter__') and not isinstance(python_object, (basestring, ParseType)):
# It's an iterable? Repeat this whole process on each object
return [ParseType.convert_to_parse(o, as_pointer=as_pointer)
for o in python_object]
Expand Down Expand Up @@ -203,6 +208,54 @@ def _to_native(self):
_absolute_url = property(lambda self: self._api_url)


@complex_type()
class ACL(ParseType):

@classmethod
def from_native(cls, **kw):
return cls(kw)

def __init__(self, acl):
self._acl = acl

def _to_native(self):
return self._acl

def __repr__(self):
return '%s(%s)' % (type(self).__name__, repr(self._acl))

def set_default(self, read=False, write=False):
self._set_permission("*", read, write)

def set_role(self, role, read=False, write=False):
if isinstance(role, ParseResource):
self._set_permissions("role:%s" % role.name, read, write)
else:
self._set_permissions("role:%s" % role, read, write)

def set_user(self, user, read=False, write=False):
if isinstance(user, ParseResource):
self._set_permission(user.objectId, read, write)
else:
self._set_permission(user, read, write)

def set_all(self, permissions):
self._acl.clear()
for k, v in permissions.items():
self._set_permission(k, **v)

def _set_permission(self, name, read=False, write=False):
permissions = {}
if read is True:
permissions["read"] = True
if write is True:
permissions["write"] = True
if len(permissions):
self._acl[name] = permissions
else:
self._acl.pop(name, None)


class Function(ParseBase):
ENDPOINT_ROOT = '/'.join((API_ROOT, 'functions'))

Expand Down Expand Up @@ -236,7 +289,7 @@ def __getattr__(self, attr):

def _init_attrs(self, args):
for key, value in six.iteritems(args):
setattr(self, key, ParseType.convert_from_parse(value))
setattr(self, key, ParseType.convert_from_parse(key, value))
self._is_loaded = True

def _to_native(self):
Expand Down
35 changes: 35 additions & 0 deletions parse_rest/role.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


from parse_rest.connection import API_ROOT
from parse_rest.datatypes import ParseResource
from parse_rest.query import QueryManager


class Role(ParseResource):
'''
A Role is like a regular Parse object (can be modified and saved) but
it requires additional methods and functionality
'''
ENDPOINT_ROOT = '/'.join([API_ROOT, 'roles'])

@property
def className(self):
return '_Role'

def __repr__(self):
return '<Role:%s (Id %s)>' % (getattr(self, 'name', None), self.objectId)


Role.Query = QueryManager(Role)

0 comments on commit 2aad3cd

Please sign in to comment.