def __init__(self, *args, **kwargs): """ Changes the order of fields, and removes the username field. """ request = kwargs.pop('request', None) provider = kwargs.pop('provider', 'local') # we only use LocalUserCreationForm for local provider if not provider == 'local': raise Exception('Invalid provider') self.ip = None if request: self.ip = request.META.get('REMOTE_ADDR', request.META.get('HTTP_X_REAL_IP', None)) super(LocalUserCreationForm, self).__init__(*args, **kwargs) self.fields.keyOrder = ['email', 'first_name', 'last_name', 'password1', 'password2'] if settings.RECAPTCHA_ENABLED: self.fields.keyOrder.extend(['recaptcha_challenge_field', 'recaptcha_response_field', ]) if get_latest_terms(): self.fields.keyOrder.append('has_signed_terms') if 'has_signed_terms' in self.fields: # Overriding field label since we need to apply a link # to the terms within the label terms_link_html = '<a href="%s" target="_blank">%s</a>' \ % (reverse('latest_terms'), _("the terms")) self.fields['has_signed_terms'].label = \ mark_safe("I agree with %s" % terms_link_html)
def handle(self, *args, **options): if len(args) != 3: raise CommandError("Invalid number of arguments") email, first_name, last_name = args[:3] password = options['password'] or \ AstakosUser.objects.make_random_password() try: validate_email(email) except ValidationError: raise CommandError("Invalid email") has_signed_terms = not (get_latest_terms()) try: user = make_local_user(email, first_name=first_name, last_name=last_name, password=password, has_signed_terms=has_signed_terms) if options['is_superuser']: user.is_superuser = True user.save() except BaseException, e: raise CommandError(e)
def handle(self, *args, **options): if len(args) != 3: raise CommandError("Invalid number of arguments") email, first_name, last_name = args[:3] password = options['password'] or \ AstakosUser.objects.make_random_password() try: validate_email(email) except ValidationError: raise CommandError("Invalid email") has_signed_terms = not(get_latest_terms()) try: user = make_local_user( email, first_name=first_name, last_name=last_name, password=password, has_signed_terms=has_signed_terms) if options['is_superuser']: user.is_superuser = True user.save() except BaseException, e: raise CommandError(e)
def __init__(self, *args, **kwargs): """ Changes the order of fields, and removes the username field. """ self.provider = kwargs.pop('provider', None) self.request = kwargs.pop('request', None) if not self.provider or self.provider == 'local': raise Exception('Invalid provider, %r' % self.provider) # ThirdPartyUserCreationForm should always get instantiated with # a third_party_token value self.third_party_token = kwargs.pop('third_party_token', None) if not self.third_party_token: raise Exception('ThirdPartyUserCreationForm' ' requires third_party_token') super(ThirdPartyUserCreationForm, self).__init__(*args, **kwargs) if not get_latest_terms(): del self.fields['has_signed_terms'] if 'has_signed_terms' in self.fields: # Overriding field label since we need to apply a link # to the terms within the label terms_link_html = '<a href="%s" target="_blank">%s</a>' \ % (reverse('latest_terms'), _("the terms")) self.fields['has_signed_terms'].label = \ mark_safe("I agree with %s" % terms_link_html)
def _handle_third_party_auto_signup(request, provider, provider_info, identifier, user_info): """Create AstakosUser for third party user without requiring signup form. Handle third party signup by automatically creating an AstakosUser. This is performed when the user's profile is automatically set by the provider. """ try: email = user_info['email'] first_name = user_info['first_name'] last_name = user_info['last_name'] except KeyError as e: raise Exception("Invalid user info. Missing '%s'", str(e)) has_signed_terms = not get_latest_terms() user = auth.make_user(email=email, first_name=first_name, last_name=last_name, has_signed_terms=has_signed_terms) provider_data = { 'affiliation': user_info.get('affiliation', provider), 'info': provider_info } provider = auth_providers.get_provider(module=provider, user_obj=user, identifier=identifier, **provider_data) provider.add_to_user() # Handle user activation activation_backend = activation_backends.get_backend() result = activation_backend.handle_registration(user) activation_backend.send_result_notifications(result, user) return user
def users_create(request): user_id = request.user_uniq req = api.utils.get_json_body(request) logger.info('users_create: %s request: %s', user_id, req) user_data = req.get('user', {}) email = user_data.get('username', None) first_name = user_data.get('first_name', None) last_name = user_data.get('last_name', None) affiliation = user_data.get('affiliation', None) password = user_data.get('password', None) metadata = user_data.get('metadata', {}) password_gen = AstakosUser.objects.make_random_password if not password: password = password_gen() try: validate_email(email) except ValidationError: raise faults.BadRequest("Invalid username (email format required)") if AstakosUser.objects.verified_user_exists(email): raise faults.Conflict("User '%s' already exists" % email) if not first_name: raise faults.BadRequest("Invalid first_name") if not last_name: raise faults.BadRequest("Invalid last_name") has_signed_terms = not(get_latest_terms()) try: user = make_local_user(email, first_name=first_name, last_name=last_name, password=password, has_signed_terms=has_signed_terms) if metadata: # we expect a unique local auth provider for the user provider = user.auth_providers.get() provider.info = metadata provider.affiliation = affiliation provider.save() user = AstakosUser.objects.get(pk=user.pk) code = user.verification_code ver_res = activation_backend.handle_verification(user, code) if ver_res.is_error(): raise Exception(ver_res.message) mod_res = activation_backend.handle_moderation(user, accept=True) if mod_res.is_error(): raise Exception(ver_res.message) except Exception, e: raise faults.BadRequest(e.message)
def __init__(self, *args, **kwargs): """ Changes the order of fields, and removes the username field. """ self.provider = kwargs.pop('provider', None) self.request = kwargs.pop('request', None) if not self.provider or self.provider == 'local': raise Exception('Invalid provider, %r' % self.provider) # ThirdPartyUserCreationForm should always get instantiated with # a third_party_token value self.third_party_token = kwargs.pop('third_party_token', None) if not self.third_party_token: raise Exception('ThirdPartyUserCreationForm' ' requires third_party_token') super(ThirdPartyUserCreationForm, self).__init__(*args, **kwargs) if not get_latest_terms(): del self.fields['has_signed_terms'] if 'has_signed_terms' in self.fields: # Overriding field label since we need to apply a link # to the terms within the label terms_link_html = '<a href="%s" target="_blank">%s</a>' \ % (reverse('latest_terms'), _("the terms")) self.fields['has_signed_terms'].label = \ mark_safe("I agree with %s" % terms_link_html) auth_provider = auth_providers.get_provider(self.provider) user_attr_map = auth_provider.get_user_attr_map() for field in ['email', 'first_name', 'last_name']: if not user_attr_map[field][1]: self.ro_fields.append(field) self.fields[field].widget.attrs['readonly'] = True self.fields[field].help_text = _(READ_ONLY_FIELD_MSG)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a user ID or email") identifier = args[0] if identifier.isdigit(): users = AstakosUser.objects.filter(id=int(identifier)) else: try: uuid.UUID(identifier) except: users = AstakosUser.objects.filter(email__iexact=identifier) else: users = AstakosUser.objects.filter(uuid=identifier) if users.count() == 0: field = "id" if identifier.isdigit() else "email" msg = "Unknown user with %s '%s'" % (field, identifier) raise CommandError(msg) for user in users: kv = OrderedDict( [ ("id", user.id), ("uuid", user.uuid), ("status", user.status_display), ("email", user.email), ("first name", user.first_name), ("last name", user.last_name), ("admin", user.is_superuser), ("last login", user.last_login), ("date joined", user.date_joined), ("last update", user.updated), # ('token', user.auth_token), ("token expiration", user.auth_token_expires), ("providers", user.auth_providers_display), ("groups", [elem.name for elem in user.groups.all()]), ("permissions", [elem.codename for elem in user.user_permissions.all()]), ("group permissions", user.get_group_permissions()), ("email_verified", user.email_verified), ("moderated", user.moderated), ("rejected", user.is_rejected), ("active", user.is_active), ("username", user.username), ("activation_sent_date", user.activation_sent), ("last_login_details", user.last_login_info_display), ] ) if get_latest_terms(): has_signed_terms = user.signed_terms kv["has_signed_terms"] = has_signed_terms if has_signed_terms: kv["date_signed_terms"] = user.date_signed_terms utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True) if options["list_quotas"] and user.is_accepted(): unit_style = options["unit_style"] check_style(unit_style) quotas = get_user_quotas(user) if quotas: self.stdout.write("\n") print_data, labels = show_user_quotas(quotas, style=unit_style) utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="User Quota") if options["list_projects"]: print_data, labels = ownerships(user) if print_data: self.stdout.write("\n") utils.pprint_table( self.stdout, print_data, labels, options["output_format"], title="Owned Projects" ) print_data, labels = memberships(user) if print_data: self.stdout.write("\n") utils.pprint_table( self.stdout, print_data, labels, options["output_format"], title="Project Memberships" )
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a user ID or email") identifier = args[0] if identifier.isdigit(): users = AstakosUser.objects.filter(id=int(identifier)) else: try: uuid.UUID(identifier) except: users = AstakosUser.objects.filter(email__iexact=identifier) else: users = AstakosUser.objects.filter(uuid=identifier) if users.count() == 0: field = 'id' if identifier.isdigit() else 'email' msg = "Unknown user with %s '%s'" % (field, identifier) raise CommandError(msg) for user in users: kv = OrderedDict([ ('id', user.id), ('uuid', user.uuid), ('status', user.status_display), ('email', user.email), ('first name', user.first_name), ('last name', user.last_name), ('admin', user.is_superuser), ('last login', user.last_login), ('date joined', user.date_joined), ('last update', user.updated), #('token', user.auth_token), ('token expiration', user.auth_token_expires), ('providers', user.auth_providers_display), ('groups', [elem.name for elem in user.groups.all()]), ('permissions', [elem.codename for elem in user.user_permissions.all()]), ('group permissions', user.get_group_permissions()), ('email_verified', user.email_verified), ('moderated', user.moderated), ('rejected', user.is_rejected), ('active', user.is_active), ('username', user.username), ('activation_sent_date', user.activation_sent), ('last_login_details', user.last_login_info_display), ]) if get_latest_terms(): has_signed_terms = user.signed_terms kv['has_signed_terms'] = has_signed_terms if has_signed_terms: kv['date_signed_terms'] = user.date_signed_terms utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True) if options["list_quotas"] and user.is_accepted(): unit_style = options["unit_style"] check_style(unit_style) quotas = get_user_quotas(user) if quotas: self.stdout.write("\n") print_data, labels = show_user_quotas(quotas, style=unit_style) utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="User Quota") if options["list_projects"]: print_data, labels = ownerships(user) if print_data: self.stdout.write("\n") utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="Owned Projects") print_data, labels = memberships(user) if print_data: self.stdout.write("\n") utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="Project Memberships")