migrate session length setting

This commit is contained in:
adamscmRH
2018-06-27 15:16:07 -04:00
parent b5dc3e6b94
commit 4c84d400a8
7 changed files with 73 additions and 20 deletions

View File

@@ -5,20 +5,18 @@ from django.utils.translation import ugettext_lazy as _
from awx.conf import fields, register
from awx.api.fields import OAuth2ProviderField
from oauth2_provider.settings import oauth2_settings
from django.conf import settings
register(
'AUTH_TOKEN_EXPIRATION',
field_class=fields.AuthTokenField,
'SESSION_COOKIE_AGE',
field_class=fields.IntegerField,
min_value=60,
max_value=30000000000, # approx 1,000 years, higher values give OverflowError
default={'AUTH_TOKEN_EXPIRATION': settings.AUTH_TOKEN_EXPIRATION},
label=_('Idle Time Force Log Out'),
help_text=_('Number of seconds that a user is inactive before they will need to login again.'),
category=_('Authentication'),
category_slug='authentication',
)
register(
'SESSIONS_PER_USER',
field_class=fields.IntegerField,

View File

@@ -6,7 +6,6 @@ from collections import OrderedDict
# Django
from django.core.validators import URLValidator
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
# Django REST Framework
from rest_framework.fields import * # noqa
@@ -43,13 +42,6 @@ class IntegerField(IntegerField):
if ret == '' and self.allow_null and not getattr(self, 'allow_blank', False):
return None
return ret
class AuthTokenField(IntegerField):
def to_internal_value(self, data):
settings.SESSION_COOKIE_AGE = data
return super(AuthTokenField, self).to_internal_value(data)
class StringListField(ListField):

View File

@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.utils import timezone
from django.db import migrations
def copy_session_settings(apps, schema_editor):
def rename_setting(old_key, new_key):
Setting = apps.get_model('conf', 'Setting')
if Setting.objects.filter(key=new_key).exists():
logger.error('Setting', new_key, 'unexpectedly exists before this migration, \
it will be replaced by the value of the', AUTH_TOKEN_EXPIRATION, 'setting.')
Setting = apps.get_model('conf', 'Setting')
mapping = {
'AUTH_TOKEN_EXPIRATION': 'SESSION_COOKIE_AGE',
'AUTH_TOKEN_PER_USER': 'SESSIONS_PER_USER'
}
for before, after in mapping.items():
...
for setting_name in mapping:
old_setting = Setting.objects.filter(key=setting_name).first()
if
if Setting.objects.filter(key='SESSION_COOKIE_AGE').exists():
logger.error('Setting SESSION_COOKIE_AGE unexpectedly exists before this migration, it will be replaced by AUTH_TOKEN_EXPIRATION setting')
if Setting.objects.filter(key='AUTH_TOKEN_EXPIRATION').exists():
Setting.objects.filter(key='SESSION_COOKIE_AGE').delete()
Setting.objects.get_or_create(key='SESSION_COOKIE_AGE',
value=Setting.objects.get(key='AUTH_TOKEN_EXPIRATION').value,
created=timezone.now(),
modified=timezone.now())
if Setting.objects.filter(key='AUTH_TOKEN_PER_USER').exists():
Setting.objects.filter(key='SESSIONS_PER_USER').delete()
Setting.objects.get_or_create(key='SESSIONS_PER_USER',
value=Setting.objects.get(key='AUTH_TOKEN_PER_USER').value,
created=timezone.now(),
modified=timezone.now())
class Migration(migrations.Migration):
dependencies = [
('conf', '0004_v320_reencrypt'),
]
operations = [
migrations.RunPython(copy_session_settings),
]

View File

@@ -200,8 +200,7 @@ SESSION_COOKIE_SECURE = True
# Seconds before sessions expire.
# Note: This setting may be overridden by database settings.
AUTH_TOKEN_EXPIRATION = 1800
SESSION_COOKIE_AGE = AUTH_TOKEN_EXPIRATION
SESSION_COOKIE_AGE = 1800
# Maximum number of per-user valid, concurrent sessions.
# -1 is unlimited

View File

@@ -24,11 +24,11 @@ export default ['i18n', function(i18n) {
MANAGE_ORGANIZATION_AUTH: {
type: 'toggleSwitch',
},
AUTH_TOKEN_EXPIRATION: {
SESSION_COOKIE_AGE: {
type: 'number',
integer: true,
min: 60,
reset: 'AUTH_TOKEN_EXPIRATION',
reset: 'SESSION_COOKIE_AGE',
},
SESSIONS_PER_USER: {
type: 'number',