Exemplo n.º 1
0
    def obj_create(self, bundle,  **kwargs):
        request = bundle.request
        LUSER = get_user(request)
        action = str(LUSER.pk)+"/" + bundle.request.POST['username']
        try:
            UserGroups.objects.get(pk=bundle.request.POST['groups'].replace('/api/v1/groups/group/','').replace('/',''),countries=LUSER.country)
        except UserGroups.DoesNotExist:
            errors = {'error':['Permission denied - Trying to register invalid group!!']}
            options = {
            'R':bundle.request,
                'A':"/Users/User/Update/Access violation(Trying to register in different group)/" +action,
                'U':LUSER,
                'L_T':'app',
                'E':errors,
                'C':500,
            }
            handle_errors(options)  
            #raise Unauthorized("Permission denied - Trying to register invalid group")

        if self.Meta.authorization.is_authorized(request,'Users.Create'):
            try:
                try:
                    from UserManagement.forms import UserCreationFormAPI
                    UserCreationFormAPI.country = LUSER.country
                    form = UserCreationFormAPI(bundle.request.POST)
                    
                    if form.is_valid():
                        superuser=False
                        if LUSER.is_superuser:
                            try:
                               superuser = (bundle.request.POST['is_superuser'] == "true")
                            except:
                                pass
                        bundle = super(CRUDUser, self).obj_create(bundle, country=LUSER.country,first_name=bundle.obj.username,is_superuser=superuser)
                        bundle.obj.save()
                        
                        from django.template import loader
                        from django.contrib.auth.tokens import default_token_generator
                        from django.contrib.sites.models import get_current_site
                        from django.utils.http import int_to_base36

                        current_site = get_current_site(request)
                        site_name = current_site.name
                        domain = current_site.domain
                        
                        CreatedUser = User.objects.get(pk=bundle.obj.id)
                        CreatedUser.first_name = bundle.obj.username;
                        CreatedUser.set_password('IkEA4102!')
                        CreatedUser.is_superuser=superuser
                        CreatedUser.save()

                        
                        use_https = False
                        c = {
                            'email': CreatedUser.email,
                            'domain': domain,
                            'site_name': site_name,
                            'uid': int_to_base36(CreatedUser.id),
                            'user': CreatedUser,
                            'token': default_token_generator.make_token(CreatedUser),
                            'protocol': use_https and 'https' or 'http',
                            'home_url': '/%s/' % CreatedUser.country.iso_code,
                        }
                        subject = loader.render_to_string('base_templates/registration/welcome_email_subject.txt', c)
                        # Email subject *must not* contain newlines
                        subject = ''.join(subject.splitlines())
                        email = loader.render_to_string('base_templates/registration/welcome_email.html', c)
                        
                        CreatedUser.email_user(subject,email,'*****@*****.**')
                        try:
                            action = "/Users/User/Create/Success/" + action
                            Logging().log(request=bundle.request,log_entry=action,User=LUSER,log_type="app",comments="Created successfully",process_code=200)
                            
                        except:
                            pass
                        
                    else:
                        errors = {'error':{}}
                        for  k, v in form.errors.items():
                            errors['error'][k] = v

                        options = {
                            'R':bundle.request,
                            'A':"/Users/User/Create/Form validation error/" +action,
                            'U':LUSER,
                            'L_T':'app',
                            'E':errors['error'],
                            'C':500,
                        }
                        handle_errors(options)                    

                except AttributeError, e:
                    errors = {'error':[str(e)]}
                    options = {
                        'R':bundle.request,
                        'A':"/Users/User/Create/Application error(attribute)/" +action,
                       'U':LUSER,
                        'L_T':'app',
                        'E':errors,
                        'C':500,
                    }
                    handle_errors(options)                    
            except IntegrityError, e:
                errors = {'error':[str(e)]}
                options = {
                    'R':bundle.request,
                    'A':"/Users/User/Create/Application error(integrity)/" +action,
                    'U':LUSER,
                    'L_T':'app',
                    'E':errors,
                    'C':500,
                }
                handle_errors(options)
Exemplo n.º 2
0
    def obj_update(self, bundle,  **kwargs):
        request = bundle.request
        LUSER = get_user(request)
        
        
        G = User.objects.get(pk=bundle.request.POST['id'])
        action =  str(LUSER.pk)+"/" +str(bundle.request.POST['id'])+"/" + bundle.request.POST['username']
        if int(bundle.request.POST['id']) == LUSER.id:
            errors = {'error':['Update your information using account info']}
            options = {
                'R':bundle.request,
                'A':"/Users/User/Update/Access violation(self user update)/" +action,
                'U':LUSER,
                'L_T':'app',
                'E':errors,
                'C':500,
            }
            handle_errors(options)                    
        if not G.country.pk == LUSER.country.pk or G.groups.is_system_group:
            if G.groups.is_system_group:
                errors = {'error':['Permission denied to modify system user!!']}
                options = {
                    'R':bundle.request,
                    'A':"/Users/User/Update/Access violation(system user update)/" +action,
                    'U':LUSER,
                    'L_T':'app',
                    'E':errors,
                    'C':500,
                }
                handle_errors(options)                    
            else:
                errors = {'error':['Permission denied to modify other system user!!']}
                options = {
                    'R':bundle.request,
                    'A':"/Users/User/Update/Access violation/" +action,
                    'U':LUSER,
                    'L_T':'app',
                    'E':errors,
                    'C':500,
                }
                handle_errors(options)                    
        
        if self.Meta.authorization.is_authorized(request,'Users.Edit'):
            try:
                try:
                    
                    from UserManagement.forms import UserCreationFormAPI
                    UserCreationFormAPI.country = LUSER.country
                    UserCreationFormAPI.isUpdate  = True
                    CreatedUser = User.objects.get(pk=bundle.request.POST['id'])
                    UserCreationFormAPI.oldName = CreatedUser.username
                    
                    form = UserCreationFormAPI(bundle.request.POST)
                    if form.is_valid():
                        bundle = super(CRUDUser, self).obj_update(bundle,**kwargs)
                        bundle.obj.save()
                        if LUSER.is_superuser:
                            CreatedUser = User.objects.get(pk=bundle.obj.id)
                            try:
                               CreatedUser.is_active = (bundle.request.PUT['is_active'] == "true")
                            except:
                                pass
                            try:
                               CreatedUser.is_superuser =  (bundle.request.PUT['is_superuser'] == "true")
                            except:
                                pass
                            CreatedUser.save()

                            try:
                                action = "/Users/User/Update/Success/" + action
                                Logging().log(request=bundle.request,log_entry=action,User=LUSER,log_type="app",comments="Updated successfully",process_code=201)
                                
                            except:
                                pass
                    else:
                        errors = {'error':{}}
                        for  k, v in form.errors.items():
                            errors['error'][k] = v

                        options = {
                            'R':bundle.request,
                            'A':"/Users/User/Update/Form validation error/" +action,
                            'U':LUSER,
                            'L_T':'app',
                            'E':errors['error'],
                            'C':500,
                        }
                        handle_errors(options)                    
                    
                except AttributeError, e:
                    errors = {'error':[str(e)]}
                    options = {
                        'R':bundle.request,
                        'A':"/Users/User/Update/Application error(attribute)/" +action,
                       'U':LUSER,
                        'L_T':'app',
                        'E':errors,
                        'C':500,
                    }
                    handle_errors(options)                    
            except IntegrityError, e1:
                errors = {'error':[str(e1)]}
                options = {
                    'R':bundle.request,
                    'A':"/Users/User/Update/Application error(integrity)/" +action,
                    'U':LUSER,
                    'L_T':'app',
                    'E':errors,
                    'C':500,
                }
                handle_errors(options)