def wrap(controller, request, *args, **kwargs): acl = ACL() if not request.user or not request.user.is_authenticated: return redirect(reverse("app.web.login") + "?redirect=" + request.get_full_path()) if permission and not acl.user_has_permission(request.user.id, permission): raise Http404("Page not found.") return function(controller, request, *args, **kwargs)
def __init__(self): self.__acl = ACL() self.__option_entity = Option_Entity() self.__user_entity = User_Entity() self.__helpers = Helpers() self.__notification_entity = Notification_Entity() self.__logger = self.__helpers.get_logger(__name__)
def __init__(self): self.__acl = ACL() self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__notification_entity = NotificationEntity() self.__register_request_entity = RegisterRequestEntity() self.__task_core = TaskCore()
def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__settings_module = Settings_Module() self.__acl = ACL() self.__logger = self.__helpers.get_logger(__name__)
def wrap(controller, request, *args, **kwargs): acl = ACL() if not request.user or not request.user.is_authenticated or not acl.user_has_permission(request.user.id, permission): response = Response() return JsonResponse(response.send_private_failure([{ "type": "error", "message": _("Oops! Access forbidden.") }])) return function(controller, request, *args, **kwargs)
def get(self, request): self.__context = Context() self.__upgrade = Upgrade() self.__acl = ACL() self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__context.autoload_options() self.__context.autoload_user( request.user.id if request.user.is_authenticated else None) self.__context.load_options({ "app_name": "", "app_email": "", "app_url": "", "app_description": "", "google_analytics_account": "", "reset_mails_messages_count": "", "reset_mails_expire_after": "", "access_tokens_expire_after": "", "prometheus_token": "", "newrelic_api_key": "" }) self.__context.push({ "current": self.__upgrade.get_current_version(), "latest": self.__upgrade.get_latest_version() }) self.__context.push({ "page_title": _("Settings · %s") % self.__context.get("app_name", os.getenv("APP_NAME", "Silverback")) }) return render(request, self.template_name, self.__context.get())
def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__settings_module = SettingsModule() self.__acl = ACL() self.__activity_module = ActivityModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules())
def get(self, request): self.__upgrade = Upgrade() self.__acl = ACL() self.autoload_options() self.autoload_user( request.user.id if request.user.is_authenticated else None) self.context_push({ "page_title": _("Activity · %s") % self.context_get("app_name", os.getenv("APP_NAME", "Silverback")) }) return render(request, self.template_name, self.context_get())
def get(self, request): self.__context = Context() self.__acl = ACL() self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__context.autoload_options() self.__context.autoload_user( request.user.id if request.user.is_authenticated else None) self.__context.push({ "page_title": _("Notification · %s") % self.__context.get("app_name", os.getenv("APP_NAME", "Silverback")) }) return render(request, self.template_name, self.__context.get())
class Activity(View): template_name = 'templates/admin/activity.html' __context = Context() __upgrade = Upgrade() __acl = ACL() @login_if_not_authenticated def get(self, request): self.__context.autoload_options() self.__context.autoload_user(request.user.id if request.user.is_authenticated else None) self.__context.push({ "page_title": _("Activity · %s") % self.__context.get("app_name", os.getenv("APP_NAME", "Kraven")) }) return render(request, self.template_name, self.__context.get())
class Settings(View): template_name = 'templates/admin/settings.html' __context = Context() __upgrade = Upgrade() __acl = ACL() @login_if_not_authenticated def get(self, request): if not self.__acl.user_has_permission(request.user.id, "manage_settings"): raise Http404("Page not found.") self.__context.autoload_options() self.__context.autoload_user( request.user.id if request.user.is_authenticated else None) self.__context.load_options({ "app_name": "", "app_email": "", "app_url": "", "app_description": "", "google_analytics_account": "", "reset_mails_messages_count": "", "reset_mails_expire_after": "", "access_tokens_expire_after": "", "prometheus_token": "" }) self.__context.push({ "current": self.__upgrade.get_current_version(), "latest": self.__upgrade.get_latest_version() }) self.__context.push({ "page_title": _("Settings · %s") % self.__context.get("app_name", os.getenv("APP_NAME", "Kraven")) }) return render(request, self.template_name, self.__context.get())
class User(): __notification_entity = None __option_entity = None __user_entity = None __acl = None __register_request_entity = None __task_core = None __register_expire_option = 24 def __init__(self): self.__acl = ACL() self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__notification_entity = NotificationEntity() self.__register_request_entity = RegisterRequestEntity() self.__task_core = TaskCore() def username_used(self, username): return False if self.__user_entity.get_one_by_username( username) is False else True def email_used(self, email): return False if self.__user_entity.get_one_by_email( email) is False else True def username_used_elsewhere(self, user_id, username): user = self.__user_entity.get_one_by_username(username) if user is False or user.id == user_id: return False return True def email_used_elsewhere(self, user_id, email): user = self.__user_entity.get_one_by_email(email) if user is False or user.id == user_id: return False return True def get_one_by_id(self, id): user = self.__user_entity.get_one_by_id(id) if not user: return False return { "id": user.id, "username": user.username, "first_name": user.first_name, "last_name": user.last_name, "email": user.email, "role": "admin" if user.is_superuser else "user", } def insert_one(self, user): return self.__user_entity.insert_one(user) def create_user(self, user_data): status = True user = self.__user_entity.insert_one({ "username": user_data["username"], "email": user_data["email"], "password": user_data["password"], "first_name": user_data["first_name"], "last_name": user_data["last_name"], "is_superuser": False, "is_active": True, "is_staff": False }) if user is not False: self.__acl.add_role_to_user("normal_user", user.id) status &= (user is not False) return status def update_one_by_id(self, id, user_data): return self.__user_entity.update_one_by_id(id, user_data) def check_register_request(self, token): request = self.__register_request_entity.get_one_by_token(token) if request is not False and timezone.now() < request.expire_at: return True return False def get_register_request_by_token(self, token): return self.__register_request_entity.get_one_by_token(token) def delete_register_request_by_token(self, token): return self.__register_request_entity.delete_one_by_token(token) def delete_register_request_by_email(self, email): return self.__register_request_entity.delete_one_by_email(email) def create_register_request(self, email, role): request = self.__register_request_entity.insert_one({ "email": email, "payload": json.dumps({"role": role}), "expire_after": self.__register_expire_option }) return request.token if request is not False else False def send_register_request_message(self, email, token): app_name = self.__option_entity.get_value_by_key("app_name") app_email = self.__option_entity.get_value_by_key("app_email") app_url = self.__option_entity.get_value_by_key("app_url") return self.__task_core.delay( "register_request_email", { "app_name": app_name, "app_email": app_email, "app_url": app_url, "recipient_list": [email], "token": token, "subject": _("%s Signup Invitation") % (app_name), "template": "mails/register_invitation.html", "fail_silently": False }, 1) def count_all(self): return self.__user_entity.count_all() def get_all(self, offset=None, limit=None): return self.__user_entity.get_all(offset, limit) def delete_one_by_id(self, id): return self.__user_entity.delete_one_by_id(id)
def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__acl = ACL()
class Install(): __options = [{ "key": "app_installed", "value": "true", "autoload": True }, { "key": "app_description", "value": "", "autoload": False }, { "key": "google_analytics_account", "value": "", "autoload": True }, { "key": "reset_mails_messages_count", "value": "5", "autoload": False }, { "key": "reset_mails_expire_after", "value": "24", "autoload": False }, { "key": "access_tokens_expire_after", "value": "48", "autoload": False }, { "key": "prometheus_token", "value": "", "autoload": False }, { "key": "newrelic_api_key", "value": "", "autoload": False }] __admin = { "username": "", "email": "", "password": "", "is_superuser": True, "is_active": True, "is_staff": False } def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__acl = ACL() def is_installed(self): return False if self.__option_entity.get_one_by_key( "app_installed") is False else True def set_app_data(self, name, email, url): self.__options.append({ "key": "app_name", "value": name, "autoload": True }) self.__options.append({ "key": "app_email", "value": email, "autoload": True }) self.__options.append({ "key": "app_url", "value": url, "autoload": True }) def set_admin_data(self, username, email, password): self.__admin["username"] = username self.__admin["email"] = email self.__admin["password"] = password def init_base_acl(self, user_id): self.__acl.truncate_default_permissions() self.__acl.new_role("super_admin") self.__acl.new_role("normal_user") self.__acl.new_permission( "Manage Settings", self.__acl.get_content_type_id("auth", "user"), "manage_settings") self.__acl.add_permission_to_role("super_admin", "manage_settings") self.__acl.add_role_to_user("super_admin", user_id) return True def install(self): execute_from_command_line(["manage.py", "migrate"]) status = True status &= self.__option_entity.insert_many(self.__options) user = self.__user_entity.insert_one(self.__admin) status &= (user is not False) if user is not False: status &= self.init_base_acl(user.id) return user.id if status else False def uninstall(self): self.__option_entity.truncate() self.__user_entity.truncate()
def wrap(controller, request, *args, **kwargs): acl = ACL() if not request.user or not request.user.is_authenticated or not acl.user_has_permission(request.user.id, permission): raise Http404("Page not found.") return function(controller, request, *args, **kwargs)
class Settings(View): __request = None __response = None __helpers = None __form = None __settings_module = None __logger = None __acl = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__settings_module = Settings_Module() self.__acl = ACL() self.__logger = self.__helpers.get_logger(__name__) def post(self, request): if not self.__acl.user_has_permission(request.user.id, "manage_settings"): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "app_name": "", "app_email": "", "app_url": "", "app_description": "", "google_analytics_account": "", "reset_mails_messages_count": "", "reset_mails_expire_after": "", "access_tokens_expire_after": "", "prometheus_token": "" }) self.__form.add_inputs({ 'app_name': { 'value': request_data["app_name"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Application name must be alpha numeric.') }, 'length_between': { 'param': [3, 10], 'error': _('Error! Application name must be 5 to 10 characters long.') } } }, 'app_email': { 'value': request_data["app_email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'email': { 'error': _('Error! Application email is invalid.') } } }, 'app_url': { 'value': request_data["app_url"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'url': { 'error': _('Error! Application url is invalid.') } } }, 'app_description': { 'value': request_data["app_description"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 300], 'error': _('Error! App description is very long.') }, 'optional': {} } }, "prometheus_token": { 'value': request_data["prometheus_token"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 100], 'error': _('Error! Prometheus token is invalid.') }, 'optional': {} } }, 'google_analytics_account': { 'value': request_data["google_analytics_account"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 30], 'error': _('Error! Google analytics account is invalid.') }, 'optional': {} } }, 'reset_mails_messages_count': { 'value': request_data["reset_mails_messages_count"], 'sanitize': { 'strip': {} }, 'validate': { 'digit': { 'error': _('Error! Reset mails count is invalid.') }, 'greater_than': { 'error': _('Error! Reset mails count is invalid.'), 'param': [0] } } }, 'reset_mails_expire_after': { 'value': request_data["reset_mails_expire_after"], 'sanitize': { 'strip': {} }, 'validate': { 'digit': { 'error': _('Error! Reset mails expiry interval is invalid.') }, 'greater_than': { 'error': _('Error! Reset mails count is invalid.'), 'param': [0] } } }, 'access_tokens_expire_after': { 'value': request_data["access_tokens_expire_after"], 'sanitize': { 'strip': {} }, 'validate': { 'digit': { 'error': _('Error! Access token expiry interval is invalid.') }, 'greater_than': { 'error': _('Error! Access token expiry interval is invalid.'), 'param': [0] } } }, }) self.__form.process() if not self.__form.is_passed(): return JsonResponse(self.__response.send_private_failure(self.__form.get_errors(with_type=True))) result = self.__settings_module.update_options({ "app_name": self.__form.get_input_value("app_name"), "app_email": self.__form.get_input_value("app_email"), "app_url": self.__form.get_input_value("app_url"), "app_description": self.__form.get_input_value("app_description"), "google_analytics_account": self.__form.get_input_value("google_analytics_account"), "reset_mails_messages_count": self.__form.get_input_value("reset_mails_messages_count"), "reset_mails_expire_after": self.__form.get_input_value("reset_mails_expire_after"), "access_tokens_expire_after": self.__form.get_input_value("access_tokens_expire_after"), "prometheus_token": self.__form.get_input_value("prometheus_token") }) if result: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Settings updated successfully.") }])) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating settings.") }]))
def __init__(self): self.__settings_module = SettingsModule() self.__acl = ACL() self.__activity_module = ActivityModule()
class Register(): __notification_entity = None __option_entity = None __user_entity = None __helpers = None __logger = None __acl = None def __init__(self): self.__acl = ACL() self.__option_entity = Option_Entity() self.__user_entity = User_Entity() self.__helpers = Helpers() self.__notification_entity = Notification_Entity() self.__logger = self.__helpers.get_logger(__name__) def username_used(self, username): return False if self.__user_entity.get_one_by_username( username) is False else True def email_used(self, email): return False if self.__user_entity.get_one_by_email( email) is False else True def create_user(self, user_data): status = True user = self.__user_entity.insert_one({ "username": user_data["username"], "email": user_data["email"], "password": user_data["password"], "first_name": user_data["first_name"], "last_name": user_data["last_name"], "is_superuser": False, "is_active": True, "is_staff": False }) if user is not False: self.__acl.add_role_to_user("normal_user", user.id) self.__notification_entity.insert_one({ "highlight": "$APP_NAME", "notification": _("Welcome, Hurry up and create your first host!"), "url": reverse('app.web.admin.hosts.list'), "type": "message", "delivered": False, "user_id": user.id, "task_id": None, "host_id": None }) status &= (user is not False) return status