'Sets an object\'s the permission levels based on the perm_spec JSON.
the mapping looks like:
\'users\': {
\'AnonymousUser\': [\'view\'],
<username>: [\'perm1\',\'perm2\',\'perm3\'],
<username2>: [\'perm1\',\'perm2\',\'perm3\']
\'groups\': [
<groupname>: [\'perm1\',\'perm2\',\'perm3\'],
<groupname2>: [\'perm1\',\'perm2\',\'perm3\'],'

def set_permissions(self, perm_spec):

    remove_object_permissions(self)
    if (('users' in perm_spec) and ('AnonymousUser' in perm_spec['users'])):
        anonymous_group = Group.objects.get(name='anonymous')
        for perm in perm_spec['users']['AnonymousUser']:
            if ((self.polymorphic_ctype.name == 'layer') and (perm in ('change_layer_data', 'change_layer_style', 'add_layer', 'change_layer', 'delete_layer'))):
                assign_perm(perm, anonymous_group, self.layer)
            else:
                assign_perm(perm, anonymous_group, self.get_self_resource())
    if ('users' in perm_spec):
        for (user, perms) in perm_spec['users'].items():
            user = get_user_model().objects.get(username=user)
            for perm in perms:
                if ((self.polymorphic_ctype.name == 'layer') and (perm in ('change_layer_data', 'change_layer_style', 'add_layer', 'change_layer', 'delete_layer'))):
                    assign_perm(perm, user, self.layer)
                else:
                    assign_perm(perm, user, self.get_self_resource())
            has_view_perms = ('view_resourcebase' in perms)
            has_download_perms = ('download_resourcebase' in perms)
            geofence_user = str(user)
            if ('AnonymousUser' in geofence_user):
                geofence_user = None
            set_geofence_owner(self, geofence_user, view_perms=has_view_perms, download_perms=has_download_perms)
    if ('groups' in perm_spec):
        for (group, perms) in perm_spec['groups'].items():
            group = Group.objects.get(name=group)
            for perm in perms:
                if ((self.polymorphic_ctype.name == 'layer') and (perm in ('change_layer_data', 'change_layer_style', 'add_layer', 'change_layer', 'delete_layer'))):
                    assign_perm(perm, group, self.layer)
                else:
                    assign_perm(perm, group, self.get_self_resource())
            has_view_perms = ('view_resourcebase' in perms)
            has_download_perms = ('download_resourcebase' in perms)
            set_geofence_group(self, str(group), view_perms=has_view_perms, download_perms=has_download_perms)
    set_owner_permissions(self)
