diff options
Diffstat (limited to 'src/account')
-rw-r--r-- | src/account/forms.py | 12 | ||||
-rw-r--r-- | src/account/migrations/0011_userprofile_ipa_username.py | 18 | ||||
-rw-r--r-- | src/account/migrations/0012_auto_20230725_1749.py | 18 | ||||
-rw-r--r-- | src/account/migrations/0013_auto_20230727_1903.py | 29 | ||||
-rw-r--r-- | src/account/models.py | 6 | ||||
-rw-r--r-- | src/account/urls.py | 6 | ||||
-rw-r--r-- | src/account/views.py | 75 |
7 files changed, 115 insertions, 49 deletions
diff --git a/src/account/forms.py b/src/account/forms.py index 28cb27d..8bacc24 100644 --- a/src/account/forms.py +++ b/src/account/forms.py @@ -15,15 +15,11 @@ from django.utils.translation import gettext_lazy as _ from account.models import UserProfile -class AccountSettingsForm(forms.ModelForm): +class AccountPreferencesForm(forms.ModelForm): class Meta: model = UserProfile - fields = ['company', 'email_addr', 'public_user', 'ssh_public_key', 'pgp_public_key', 'timezone'] + fields = ['timezone', 'public_user'] labels = { - 'email_addr': _('Email Address'), - 'ssh_public_key': _('SSH Public Key'), - 'pgp_public_key': _('PGP Public Key'), - 'public_user': _('Public User') } - - timezone = forms.ChoiceField(choices=[(x, x) for x in pytz.common_timezones], initial='UTC') + timezone = forms.ChoiceField(widget=forms.Select(attrs={'style': 'width: 200px;', 'class': 'form-control'}) ,choices=[(x, x) for x in pytz.common_timezones], initial='UTC') + public_user = forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={}))
\ No newline at end of file diff --git a/src/account/migrations/0011_userprofile_ipa_username.py b/src/account/migrations/0011_userprofile_ipa_username.py new file mode 100644 index 0000000..25cf6fb --- /dev/null +++ b/src/account/migrations/0011_userprofile_ipa_username.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2 on 2023-07-24 20:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0010_auto_20230608_1913'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='ipa_username', + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/src/account/migrations/0012_auto_20230725_1749.py b/src/account/migrations/0012_auto_20230725_1749.py new file mode 100644 index 0000000..3f4d142 --- /dev/null +++ b/src/account/migrations/0012_auto_20230725_1749.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2 on 2023-07-25 17:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0011_userprofile_ipa_username'), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='ipa_username', + field=models.CharField(blank=True, max_length=100, null=True), + ), + ] diff --git a/src/account/migrations/0013_auto_20230727_1903.py b/src/account/migrations/0013_auto_20230727_1903.py new file mode 100644 index 0000000..9e1d222 --- /dev/null +++ b/src/account/migrations/0013_auto_20230727_1903.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2 on 2023-07-27 19:03 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0012_auto_20230725_1749'), + ] + + operations = [ + migrations.RemoveField( + model_name='userprofile', + name='company', + ), + migrations.RemoveField( + model_name='userprofile', + name='jira_url', + ), + migrations.RemoveField( + model_name='userprofile', + name='pgp_public_key', + ), + migrations.RemoveField( + model_name='userprofile', + name='ssh_public_key', + ), + ] diff --git a/src/account/models.py b/src/account/models.py index f1deca7..bb1cad5 100644 --- a/src/account/models.py +++ b/src/account/models.py @@ -39,20 +39,16 @@ class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) timezone = models.CharField(max_length=100, blank=False, default='UTC') - ssh_public_key = models.FileField(upload_to=upload_to, null=True, blank=True) - pgp_public_key = models.FileField(upload_to=upload_to, null=True, blank=True) email_addr = models.CharField(max_length=300, blank=False, default='email@mail.com') - company = models.CharField(max_length=200, blank=False) oauth_token = models.CharField(max_length=1024, blank=False) oauth_secret = models.CharField(max_length=1024, blank=False) - jira_url = models.CharField(max_length=100, null=True, blank=True, default='') - full_name = models.CharField(max_length=100, null=True, blank=True, default='') booking_privledge = models.BooleanField(default=False) public_user = models.BooleanField(default=False) + ipa_username = models.CharField(max_length=100, null=True, blank=True) class Meta: db_table = 'user_profile' diff --git a/src/account/urls.py b/src/account/urls.py index 23ce122..35ef43b 100644 --- a/src/account/urls.py +++ b/src/account/urls.py @@ -29,24 +29,22 @@ from django.conf.urls import url from django.urls import path from account.views import ( - AccountSettingsView, OIDCLoginView, LogoutView, - UserListView, account_resource_view, account_booking_view, account_detail_view, template_delete_view, booking_cancel_view, + account_settings_view ) app_name = 'account' urlpatterns = [ - url(r'^settings/', AccountSettingsView.as_view(), name='settings'), + url(r'^settings/', account_settings_view, name='settings'), url(r'^login/$', OIDCLoginView.as_view(), name='login'), url(r'^logout/$', LogoutView.as_view(), name='logout'), - url(r'^users/$', UserListView.as_view(), name='users'), url(r'^my/resources/$', account_resource_view, name='my-resources'), path('my/resources/delete/<int:resource_id>', template_delete_view), url(r'^my/bookings/$', account_booking_view, name='my-bookings'), diff --git a/src/account/views.py b/src/account/views.py index 2d280cd..a975a2e 100644 --- a/src/account/views.py +++ b/src/account/views.py @@ -20,38 +20,55 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.models import User from django.urls import reverse from django.http import HttpResponse -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect, render from django.utils.decorators import method_decorator from django.views.generic import RedirectView, TemplateView, UpdateView from django.shortcuts import render +from api.utils import ipa_set_ssh, ipa_query_user, ipa_set_company +from dashboard.forms import SetCompanyForm, SetSSHForm from rest_framework.authtoken.models import Token from mozilla_django_oidc.auth import OIDCAuthenticationBackend -from account.forms import AccountSettingsForm +from account.forms import AccountPreferencesForm from account.models import UserProfile from booking.models import Booking from api.views import delete_template, liblaas_templates -@method_decorator(login_required, name='dispatch') -class AccountSettingsView(UpdateView): - model = UserProfile - form_class = AccountSettingsForm - template_name_suffix = '_update_form' - - def get_success_url(self): - messages.add_message(self.request, messages.INFO, - 'Settings saved') - return '/' +from workflow.views import login + +def account_settings_view(request): + if request.method == "GET": + if not request.user.is_authenticated: + return login(request) + profile = UserProfile.objects.get(user=request.user) + if (not profile or profile.ipa_username == "" or profile.ipa_username == None): + return redirect("dashboard:index") + ipa_user = ipa_query_user(profile.ipa_username) + template = "account/settings.html" + context = { + "preference_form": AccountPreferencesForm(instance=profile), + "company_form": SetCompanyForm(initial={'company': ipa_user['ou']}), + "existing_keys": ipa_user['ipasshpubkey'] if 'ipasshpubkey' in ipa_user else [] + } + return render(request, template, context) + + if request.method == 'POST': + data = request.POST - def get_object(self, queryset=None): - return self.request.user.userprofile + print("data is", data) + # User profile + profile = UserProfile.objects.get(user=request.user) + profile.public_user = "public_user" in data + profile.timezone = data["timezone"] + profile.save() - def get_context_data(self, **kwargs): - token, created = Token.objects.get_or_create(user=self.request.user) - context = super(AccountSettingsView, self).get_context_data(**kwargs) - context.update({'title': "Settings", 'token': token}) - return context + # IPA + ipa_set_company(profile, data['company']) + ipa_set_ssh(profile, data['ssh_key_list'].split(",")) + return redirect("account:settings") + + return HttpResponse(status=405) class MyOIDCAB(OIDCAuthenticationBackend): def filter_users_by_claims(self, claims): @@ -106,17 +123,6 @@ class LogoutView(LoginRequiredMixin, RedirectView): return '/' -@method_decorator(login_required, name='dispatch') -class UserListView(TemplateView): - template_name = "account/user_list.html" - - def get_context_data(self, **kwargs): - users = UserProfile.objects.filter(public_user=True).select_related('user') - context = super(UserListView, self).get_context_data(**kwargs) - context.update({'title': "Dashboard Users", 'users': users}) - return context - - def account_detail_view(request): template = "account/details.html" return render(request, template) @@ -134,10 +140,12 @@ def account_resource_view(request): template = "account/resource_list.html" if request.method == "GET": - + profile = UserProfile.objects.get(user=request.user) + if (not profile or profile.ipa_username == "" or profile.ipa_username == None): + return redirect("dashboard:index") r = liblaas_templates(request) usable_templates = r.json() - user_templates = [ t for t in usable_templates if t["owner"] == str(request.user)] + user_templates = [ t for t in usable_templates if t["owner"] == profile.ipa_username] context = { "templates": user_templates, "title": "My Resources" @@ -153,6 +161,9 @@ def account_resource_view(request): def account_booking_view(request): if not request.user.is_authenticated: return render(request, "dashboard/login.html", {'title': 'Authentication Required'}) + profile = UserProfile.objects.get(user=request.user) + if (not profile or profile.ipa_username == "" or profile.ipa_username == None): + return redirect("dashboard:index") template = "account/booking_list.html" bookings = list(Booking.objects.filter(owner=request.user, end__gt=timezone.now()).order_by("-start")) my_old_bookings = Booking.objects.filter(owner=request.user, end__lt=timezone.now()).order_by("-start") |