def signup(request): if request.method == 'POST': user_name = request.POST.get('username', None) user_password = request.POST.get('userpassword', None) user_mail = request.POST.get('usermail', None) if User.objects.filter(email=user_mail).exists(): return redirect("/?error=email") if User.objects.filter(username=user_name).exists(): return redirect("/?error=username") user = User.objects.create_user(user_name, user_mail, user_password) #Includes in group regular users group, created = Group.objects.get_or_create(name='Users') if created: utils.add_permission_to_group(Permission.objects.get(codename='add_project'), group) utils.add_permission_to_group(Permission.objects.get(codename='change_project'), group) utils.add_permission_to_group(Permission.objects.get(codename='delete_project'), group) utils.add_user_to_group(user,group) user.save() user_profile = UserProfile() user_profile.user = user user_profile.evernote_profile = EvernoteProfile() user_profile.dropbox_profile = DropboxProfile() user_profile.is_dropbox_synced, user_profile.is_evernote_synced = False, False user_profile.invitations = [] user_profile.save() user = authenticate(username = user_name, password= user_password) login(request, user) return redirect("/")
def edit_role(user, role_name): """ This function adds/removes an user from the group passed as parameter """ admin = user_in_group(user, 'Admin') blogger = user_in_group(user, 'Blogger') if (role_name == 'admin'): group = Group.objects.get(name='Admin') role = admin aux = blogger aux_name = 'Blogger' else: group = Group.objects.get(name='Blogger') role = blogger aux = admin aux_name = 'Admin' if (role): # remove user from the group if the user already exists in the group groups = [] groups.append(Group.objects.get(name='Reader')) if (aux): groups.append(Group.objects.get(name=aux_name)) utils.update_user_groups(user, groups) else: # add user to the group utils.add_user_to_group(user, group)
def setUp(self): self.group, created = Group.objects.get_or_create(name='jackGroup') self.user, created = User.objects.get_or_create(username='******') add_user_to_group(self.user, self.group) self.ctype = ContentType.objects.create(name='foo', model='bar', app_label='fake-for-guardian-tests') self.anonymous_user, created = User.objects.get_or_create( id=settings.ANONYMOUS_USER_ID, username='******')
def setUp(self): self.user = User.objects.get(username='******') self.group, created = Group.objects.get_or_create(name='jackGroup') add_user_to_group(self.user, self.group) self.ctype = ContentType.objects.create(name='foo', model='bar', app_label='fake-for-guardian-tests') self.obj1 = ContentType.objects.create(name='ct1', model='foo', app_label='guardian-tests') self.obj2 = ContentType.objects.create(name='ct2', model='bar', app_label='guardian-tests')
def test_mixed(self): add_user_to_group(self.user1, self.group1) assign("change_contenttype", self.group1, self.obj1) assign("change_contenttype", self.user2, self.obj1) assign("delete_contenttype", self.user2, self.obj1) assign("delete_contenttype", self.user2, self.obj2) assign("change_contenttype", self.user3, self.obj2) assign("change_user", self.user3, self.user1) result = get_users_with_perms(self.obj1) self.assertEqual( set(result), set([self.user1, self.user2]), )
def load_data(request): #Groups of users #TODO check if there are no groups admin = Group(name="Admin") mo = Group(name="MO") broker = Group(name="Broker") engine = Group(name="Engine") admin.save() mo.save() broker.save() engine.save() #TODO change this by functionality of admin user_admin = User.objects.create_user('Admin', '*****@*****.**', 'Vichara') user_admin.is_staff = True user_admin.save() #user_admin.groups.add(admin) utils.add_user_to_group(user_admin, admin) #Creating Mortgage Operators Users user = User.objects.create_user('ABC Mortgage', '*****@*****.**', '1234') user.is_staff = True user.save() #user.groups.add(mo) utils.add_user_to_group(user, mo) user = User.objects.create_user('Prime Lending', '*****@*****.**', '1234') user.is_staff = True user.save() #user.groups.add(mo) utils.add_user_to_group(user, mo) user = User.objects.create_user('Best Loans Inc', '*****@*****.**', '1234') user.is_staff = True user.save() #user.groups.add(mo) utils.add_user_to_group(user, mo) user = User.objects.create_user('Integrity Lending', '*****@*****.**', '1234') user.is_staff = True user.save() #user.groups.add(mo) utils.add_user_to_group(user, mo) return HttpResponse("Data had been loaded")
def test_redirection(self): request = self._get_request(self.user) foo = User.objects.create(username='******') foobar = Group.objects.create(name='foobar') add_user_to_group(foo, foobar) @permission_required('auth.change_group', (Group, 'name', 'group_name'), login_url='/foobar/') def dummy_view(request, group_name): pass response = dummy_view(request, group_name='foobar') self.assertTrue(isinstance(response, HttpResponseRedirect)) self.assertTrue(response._headers['location'][1].startswith( '/foobar/'))
def test_get_for_object(self): group = Group.objects.create(name='get_group_perms_for_object') add_user_to_group(self.user, group) perms = GroupObjectPermission.objects.get_for_object(group, self.ctype) self.assertEqual(perms.count(), 0) to_assign = sorted([ 'delete_contenttype', 'change_contenttype', ]) for perm in to_assign: GroupObjectPermission.objects.assign(perm, group, self.ctype) perms = list(GroupObjectPermission.objects.get_for_object(group, self.ctype)\ .values_list('permission', flat=True)) codenames = sorted(Permission.objects.filter(pk__in=perms)\ .values_list('codename', flat=True)) self.assertEqual(to_assign, codenames)
def test_attach_perms(self): add_user_to_group(self.user1, self.group1) add_user_to_group(self.user2, self.group2) add_user_to_group(self.user3, self.group3) assign("change_contenttype", self.group1, self.obj1) assign("change_contenttype", self.group2, self.obj1) assign("delete_contenttype", self.group3, self.obj2) assign("delete_contenttype", self.user2, self.obj1) assign("change_contenttype", self.user3, self.obj2) # Check contenttype1 result = get_users_with_perms(self.obj1, attach_perms=True) expected = { self.user1: ["change_contenttype"], self.user2: ["change_contenttype", "delete_contenttype"], } self.assertEqual(result.keys(), expected.keys()) for key, perms in result.iteritems(): self.assertEqual(set(perms), set(expected[key])) # Check contenttype2 result = get_users_with_perms(self.obj2, attach_perms=True) expected = { self.user3: ["change_contenttype", "delete_contenttype"], } self.assertEqual(result.keys(), expected.keys()) for key, perms in result.iteritems(): self.assertEqual(set(perms), set(expected[key]))
def test_groups_perms(self): group1 = Group.objects.create(name='group1') group2 = Group.objects.create(name='group2') group3 = Group.objects.create(name='group3') groups = [group1, group2, group3] for group in groups: add_user_to_group(self.user, group) # Objects to operate on ctypes = list(ContentType.objects.all()) assign('change_contenttype', self.user, ctypes[1]) assign('change_contenttype', self.user, ctypes[2]) assign('delete_contenttype', self.user, ctypes[2]) assign('delete_contenttype', self.user, ctypes[3]) assign('change_contenttype', groups[0], ctypes[4]) assign('change_contenttype', groups[1], ctypes[4]) assign('change_contenttype', groups[2], ctypes[5]) assign('delete_contenttype', groups[0], ctypes[1]) objects = get_objects_for_user(self.user, ['contenttypes.change_contenttype']) self.assertEqual( set(objects.values_list('pk', flat=True)), set([ctypes[1].pk, ctypes[2].pk, ctypes[4].pk, ctypes[5].pk])) objects = get_objects_for_user(self.user, ['contenttypes.change_contenttype', 'contenttypes.delete_contenttype']) self.assertEqual( set(objects.values_list('pk', flat=True)), set([ctypes[1].pk, ctypes[2].pk])) objects = get_objects_for_user(self.user, ['contenttypes.change_contenttype']) self.assertEqual( set(objects.values_list('pk', flat=True)), set([ctypes[1].pk, ctypes[2].pk, ctypes[4].pk, ctypes[5].pk]))
def signup(request): if request.method == 'POST': # if POST save the article form = UserCreationForm(request.POST) if form.is_valid(): try: user = form.save() group = Group.objects.get(name='Reader') utils.add_user_to_group(user, group) return render_to_response('signup_sucess.html', context_instance=RequestContext(request)) except: pass else: # if GET create user form form = UserCreationForm() data = { 'form': form, 'admin': user_in_group(request.user, 'Admin'), 'blogger': user_in_group(request.user, 'Blogger'), } return render_to_response('signup.html', data, context_instance=RequestContext(request))
def test_users_groups_perms(self): add_user_to_group(self.user1, self.group1) add_user_to_group(self.user2, self.group2) add_user_to_group(self.user3, self.group3) assign("change_contenttype", self.group1, self.obj1) assign("change_contenttype", self.group2, self.obj1) assign("delete_contenttype", self.group3, self.obj2) result = get_users_with_perms(self.obj1).values_list('id', flat=True) self.assertEqual( set(result), set([u.id for u in (self.user1, self.user2)]) )
def test_attach_groups_only_has_perms(self): add_user_to_group(self.user1, self.group1) assign("change_contenttype", self.group1, self.obj1) result = get_users_with_perms(self.obj1, attach_perms=True) expected = {self.user1: ["change_contenttype"]} self.assertEqual(result, expected)
def setup_dev_env(app_name, username, database=None, project_name=None, project_url=None, admin_url=None, base_monthly_cost=0): try: app = Application.objects.using(UMBRELLA).get(name=app_name) app.save(using='default') except Application.DoesNotExist: slug = slugify(app_name) app = Application.objects.using(UMBRELLA).create( name=app_name, slug=slug, version='1.0', url=slug + '.com', base_monthly_cost=base_monthly_cost) app.save(using='default') app = Application.objects.using(UMBRELLA).get(pk=app.id) m = Member.objects.using(UMBRELLA).get(username=username) if not project_name: project_name = app_name project_name_slug = slugify(project_name).replace('-', '') if not database: database = project_name_slug domain = project_name_slug + '.test' if project_url: url = project_url else: url = 'http://localhost/' + project_name_slug if not admin_url: admin_url = 'http://localhost/ikwen' s = Service.objects.using(UMBRELLA).create( app=app, member=m, project_name=project_name, database=database, url=url, admin_url=admin_url, project_name_slug=project_name_slug, monthly_cost=base_monthly_cost, billing_cycle=Service.MONTHLY, domain=domain, version=Service.FREE) s.save(using='default') link = s.url.replace('http://', '').replace('https://', '') mail_signature = "%s<br>" \ "<a href='%s'>%s</a>" % (s.project_name, s.url, link) config_model = get_config_model() c = config_model.objects.using(UMBRELLA).create( service=s, company_name=project_name, contact_email=m.email, company_name_slug=project_name_slug, signature=mail_signature) c.save(using='default') InvoicingConfig.objects.create(service=s) FlatPage.objects.get_or_create(url=FlatPage.AGREEMENT, title=FlatPage.AGREEMENT) FlatPage.objects.get_or_create(url=FlatPage.LEGAL_MENTIONS, title=FlatPage.LEGAL_MENTIONS) Group.objects.get_or_create(name=COMMUNITY) sudo_group, change = Group.objects.get_or_create(name=SUDO) UserPermissionList.objects.get_or_create(user=m) add_user_to_group(m, sudo_group) if s.id not in m.collaborates_on_fk_list: m.collaborates_on_fk_list.append(s.id) if s not in m.customer_on_fk_list: m.customer_on.append(s.id) m.is_superuser = True m.is_staff = True m.is_iao = True m.is_bao = True m.save() m.business_notices = 0 m.personal_notices = 0 m.save(using='default') print("\nDev environment successfully created.") print("Add this to your project settings: IKWEN_SERVICE_ID = '%s'\n\n" % s.pk)