'Sets owner of the Object to the given user
:param User:'

def set_user(self, user):

    if (user is None):
        return self
    if isinstance(user, django_auth.User):
        user = user.profile
    elif (not isinstance(user, User)):
        raise ValueError(('user    argument    should    be    either    a    django_auth.User    or    treeio.core.User    object    got    %s' % type(user)))
    try:
        conf = ModuleSetting.get_for_module('treeio.core', 'default_permissions')[0]
        default_permissions = conf.value
    except:
        default_permissions = settings.HARDTREE_DEFAULT_PERMISSIONS
    if (not self.creator):
        self.creator = user
        self.save()
    if (default_permissions == 'everyone'):
        return self
    if ('force' in default_permissions):
        do_user = True
    else:
        cascade = getattr(self, 'access_inherit', [])
        do_user = False
        if cascade:
            for attr in cascade:
                if (attr == '*user'):
                    do_user = True
                    break
                elif (attr == '*module'):
                    if ('nomodule' not in default_permissions):
                        modules = Module.objects.filter(name__contains=self.get_object_module())
                        if modules:
                            for module in modules:
                                self.copy_permissions(module)
                            if (not user.has_permission(self, 'w')):
                                do_user = True
                            break
                    else:
                        do_user = True
                        break
                else:
                    try:
                        obj = getattr(self, attr)
                        if obj:
                            self.copy_permissions(obj)
                            if (not user.has_permission(self, 'w')):
                                do_user = True
                            break
                    except:
                        pass
    if do_user:
        if ('readonly' in default_permissions):
            access_container = self.read_access
        else:
            access_container = self.full_access
        if ('user' in default_permissions):
            access_container.add(user)
        if (('usergroup' in default_permissions) and user.default_group):
            access_container.add(user.default_group)
        if ('userallgroups' in default_permissions):
            if user.default_group:
                access_container.add(user.default_group)
            for group in user.other_groups.all():
                access_container.add(group)
    if hasattr(self, 'assigned'):
        for obj in self.assigned.all():
            if (isinstance(obj, AccessEntity) or isinstance(obj, User)):
                try:
                    if (not obj.has_permission(self, mode='w')):
                        self.full_access.add(obj)
                except:
                    pass
            else:
                try:
                    if (not obj.related_user.has_permission(self, mode='w')):
                        self.full_access.add(obj.related_user)
                except:
                    pass
    return self
