2015-04-05 22:34:47 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-04-05 22:34:47 +00:00
|
|
|
|
2015-05-12 12:38:40 +00:00
|
|
|
from orchestra.contrib.orchestration import Operation
|
|
|
|
|
|
|
|
|
2016-02-09 12:17:42 +00:00
|
|
|
def validate_paths_exist(user, paths):
|
|
|
|
operations = []
|
2016-04-15 09:56:10 +00:00
|
|
|
user.paths_to_validate = paths
|
|
|
|
operations.extend(Operation.create_for_action(user, 'validate_paths_exist'))
|
2016-02-09 12:17:42 +00:00
|
|
|
logs = Operation.execute(operations)
|
|
|
|
stderr = '\n'.join([log.stderr for log in logs])
|
|
|
|
if 'path does not exists' in stderr:
|
|
|
|
raise ValidationError(stderr)
|
2015-05-12 12:38:40 +00:00
|
|
|
|
2015-04-05 22:34:47 +00:00
|
|
|
|
|
|
|
def validate_home(user, data, account):
|
|
|
|
""" validates home based on account and data['shell'] """
|
|
|
|
if not 'username' in data and not user.pk:
|
|
|
|
# other validation will have been raised for required username
|
|
|
|
return
|
|
|
|
user = type(user)(
|
|
|
|
username=data.get('username') or user.username,
|
|
|
|
shell=data.get('shell') or user.shell,
|
|
|
|
)
|
|
|
|
if 'home' in data and data['home']:
|
|
|
|
home = os.path.normpath(data['home'])
|
|
|
|
user_home = user.get_base_home()
|
|
|
|
account_home = account.main_systemuser.get_home()
|
|
|
|
if user.has_shell:
|
|
|
|
if home != user_home:
|
|
|
|
raise ValidationError({
|
|
|
|
'home': _("Not a valid home directory.")
|
|
|
|
})
|
|
|
|
elif home not in (user_home, account_home):
|
|
|
|
raise ValidationError({
|
|
|
|
'home': _("Not a valid home directory.")
|
|
|
|
})
|
2015-05-12 12:38:40 +00:00
|
|
|
if 'directory' in data and data['directory']:
|
|
|
|
path = os.path.join(data['home'], data['directory'])
|
|
|
|
try:
|
2016-04-15 09:56:10 +00:00
|
|
|
validate_paths_exist(user, (path,))
|
2015-05-12 12:38:40 +00:00
|
|
|
except ValidationError as err:
|
|
|
|
raise ValidationError({
|
|
|
|
'directory': err,
|
|
|
|
})
|