class Login(forms.SelfHandlingForm): username = forms.CharField(max_length="20", label="User Name") password = forms.CharField(max_length="20", label="Password", widget=forms.PasswordInput(render_value=False)) def handle(self, request, data): try: token = api.token_create(request, data.get('tenant', ''), data['username'], data['password']) info = api.token_info(request, token) request.session['token'] = token.id request.session['user'] = info['user'] request.session['tenant'] = data.get('tenant', info['tenant']) request.session['admin'] = info['admin'] request.session['serviceCatalog'] = token.serviceCatalog LOG.info('Login form for user "%s". Service Catalog data:\n%s' % (data['username'], token.serviceCatalog)) return shortcuts.redirect('dash_overview') except api_exceptions.Unauthorized as e: msg = 'Error authenticating: %s' % e.message LOG.error(msg, exc_info=True) messages.error(request, msg) except api_exceptions.ApiException as e: messages.error( request, 'Error authenticating with keystone: %s' % e.message)
class Login(forms.SelfHandlingForm): username = forms.CharField(max_length="255", label="User Name") password = forms.CharField(max_length="255", label="Password", widget=forms.PasswordInput(render_value=False)) def handle(self, request, data): # retrieve endpoints if getattr(settings, "KEYSTONE_USE_LOCAL_FOR_ENDPOINTS_ONLY", False): token = util.auth_with_token( request, data, getattr(settings, "KEYSTONE_ADMIN_TOKEN", '')) else: token = util.auth(request, data, data.get('region'), True) if not token: request.session.clear() return shortcuts.redirect('auth_login') # set default service catalog util.set_default_service_catalog(request, token.serviceCatalog) # region results = util.get_regions(request) LOG.info('results %s' % results) tokens = [] for result in results: data['region'] = result token = util.auth(request, data, result, True) if token: tokens.append(result) if not tokens: request.session.clear() return shortcuts.redirect('auth_login') default_region = getattr(settings, 'SWIFT_DEFAULT_REGION', None) if default_region and api.token_for_region(request, default_region): request.session['region'] = default_region if not request.session.get('region', None): request.session['region'] = tokens[0] tenant = util.get_tenant_for_region(request) util.set_default_for_region(request) api.check_services_for_region(request) if not tenant: return shortcuts.redirect('dash_startup') return shortcuts.redirect('dash_containers', tenant)
class UserForm(forms.Form): def __init__(self, *args, **kwargs): tenant_list = kwargs.pop('tenant_list', None) super(UserForm, self).__init__(*args, **kwargs) self.fields['tenant_id'].choices = [[tenant.id, tenant.id] for tenant in tenant_list] id = forms.CharField(label="ID (username)") email = forms.CharField(label="Email") password = forms.CharField(label="Password", widget=forms.PasswordInput(render_value=False), required=False) tenant_id = forms.ChoiceField(label="Primary Tenant")
class UserUpdateForm(forms.Form): def __init__(self, *args, **kwargs): tenant_list = kwargs.pop('tenant_list', None) super(UserUpdateForm, self).__init__(*args, **kwargs) self.fields['tenant_id'].choices = [[tenant.id, tenant.name] for tenant in tenant_list] id = forms.CharField(label="ID", widget=forms.TextInput(attrs={'readonly': 'readonly'})) # FIXME: keystone doesn't return the username from a get API call. #name = forms.CharField(label="Name") email = forms.CharField(label="Email") password = forms.CharField(label="Password", widget=forms.PasswordInput(render_value=False), required=False) tenant_id = forms.ChoiceField(label="Primary Tenant")
class Login(forms.SelfHandlingForm): username = forms.CharField(max_length="20", label=_("User Name")) password = forms.CharField(max_length="20", label=_("Password"), widget=forms.PasswordInput(render_value=False)) def handle(self, request, data): def is_admin(token): for role in token.user['roles']: if role['name'].lower() == 'admin': return True return False try: if data.get('tenant'): token = api.token_create(request, data.get('tenant'), data['username'], data['password']) tenants = api.tenant_list_for_token(request, token.id) tenant = None for t in tenants: if t.id == data.get('tenant'): tenant = t else: token = api.token_create(request, '', data['username'], data['password']) # Unscoped token request.session['unscoped_token'] = token.id request.user.username = data['username'] def get_first_tenant_for_user(): tenants = api.tenant_list_for_token(request, token.id) return tenants[0] if len(tenants) else None # Get the tenant list, and log in using first tenant # FIXME (anthony): add tenant chooser here? tenant = get_first_tenant_for_user() # Abort if there are no valid tenants for this user if not tenant: messages.error(request, _('No tenants present for user: %(user)s') % {"user": data['username']}) return # Create a token token = api.token_create_scoped(request, tenant.id, token.id) request.session['admin'] = is_admin(token) request.session['serviceCatalog'] = token.serviceCatalog LOG.info('Login form for user "%s". Service Catalog data:\n%s' % (data['username'], token.serviceCatalog)) request.session['tenant'] = tenant.name request.session['tenant_id'] = tenant.id request.session['token'] = token.id request.session['user'] = data['username'] return shortcuts.redirect('dash_overview') except api_exceptions.Unauthorized as e: msg = _('Error authenticating: %s') % e.message LOG.exception(msg) messages.error(request, msg) except api_exceptions.ApiException as e: messages.error(request, _('Error authenticating with keystone: %s') % e.message)