1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 23:55:24 +00:00
webvirtcloud/accounts/apps.py

64 lines
2.2 KiB
Python
Raw Normal View History

2020-05-28 12:19:25 +00:00
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def apply_change_password(sender, **kwargs):
2022-11-02 05:54:35 +00:00
"""
2020-05-28 12:19:25 +00:00
Apply new change_password permission for all users
Depending on settings SHOW_PROFILE_EDIT_PASSWORD
2022-11-02 05:54:35 +00:00
"""
2020-05-28 12:19:25 +00:00
from django.conf import settings
from django.contrib.auth.models import Permission, User
2022-11-02 05:54:35 +00:00
if hasattr(settings, "SHOW_PROFILE_EDIT_PASSWORD"):
print("\033[1m! \033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py\033[0m")
print("\033[1m* \033[92mApplying permission can_change_password for all users\033[0m")
2020-05-28 12:19:25 +00:00
users = User.objects.all()
2022-11-02 05:54:35 +00:00
permission = Permission.objects.get(codename="change_password")
2020-05-28 12:19:25 +00:00
if settings.SHOW_PROFILE_EDIT_PASSWORD:
2022-11-02 05:54:35 +00:00
print("\033[1m! \033[91mWarning!!! Setting to True for all users\033[0m")
2020-05-28 12:19:25 +00:00
for user in users:
user.user_permissions.add(permission)
else:
2022-11-02 05:54:35 +00:00
print("\033[1m* \033[91mWarning!!! Setting to False for all users\033[0m")
2020-05-28 12:19:25 +00:00
for user in users:
user.user_permissions.remove(permission)
2022-11-02 05:54:35 +00:00
print("\033[1m! Don`t forget to remove the option from settings.py\033[0m")
2020-05-28 12:19:25 +00:00
def create_admin(sender, **kwargs):
2022-11-02 05:54:35 +00:00
"""
2020-05-28 12:19:25 +00:00
Create initial admin user
2022-11-02 05:54:35 +00:00
"""
from django.contrib.auth.models import User
2020-05-28 12:19:25 +00:00
2022-11-02 13:05:41 +00:00
from accounts.models import UserAttributes
2022-11-02 05:54:35 +00:00
plan = kwargs.get("plan", [])
2020-05-28 12:19:25 +00:00
for migration, rolled_back in plan:
2022-11-02 05:54:35 +00:00
if (
migration.app_label == "accounts"
and migration.name == "0001_initial"
and not rolled_back
):
2020-05-28 12:19:25 +00:00
if User.objects.count() == 0:
2022-11-02 05:54:35 +00:00
print("\033[1m* \033[92mCreating default admin user\033[0m")
admin = User.objects.create_superuser("admin", None, "admin")
UserAttributes(
user=admin,
max_instances=-1,
max_cpus=-1,
max_memory=-1,
max_disk_size=-1,
).save()
2020-05-28 12:19:25 +00:00
break
class AccountsConfig(AppConfig):
2022-11-02 05:54:35 +00:00
name = "accounts"
verbose_name = "Accounts"
2020-05-28 12:19:25 +00:00
def ready(self):
post_migrate.connect(create_admin, sender=self)
post_migrate.connect(apply_change_password, sender=self)