class Login(): __option_entity = None __user_entity = None def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() def is_authenticated(self, request): if request.user and request.user.is_authenticated: return True else: return False def authenticate(self, username_email, password, request=None, with_login=True): is_email = False try: is_email = True if validate_email( username_email) is None else False except Exception: is_email = False if is_email: user = self.__user_entity.get_one_by_email(username_email) if user is not False and user.check_password(password) is True: if with_login: self.login(request, user) return True else: return False else: user = authenticate(request=request, username=username_email, password=password) if user is not None: if with_login: self.login(request, user) return True else: return False def login(self, request, user): return login(request, user)
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)
class ForgotPassword(): __reset_expire_option = 24 __messages_count_option = 5 def __init__(self): self.__reset_request_entity = ResetRequestEntity() self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__task_core = TaskCore() messages_count_option = self.__option_entity.get_one_by_key( "reset_mails_messages_count") reset_expire_option = self.__option_entity.get_one_by_key( "reset_mails_expire_after") if messages_count_option: self.__messages_count_option = int(messages_count_option.value) if reset_expire_option: self.__reset_expire_option = int(reset_expire_option.value) def check_email(self, email): return True if self.__user_entity.get_one_by_email( email) is not False else False def reset_request_exists(self, email): return self.__reset_request_entity.get_one_by_email(email) def is_spam(self, request): if request.messages_count >= self.__messages_count_option and timezone.now( ) < request.expire_at: return True return False def update_request(self, request): # Delete Old Request self.__reset_request_entity.delete_one_by_id(request.id) # Create a Fresh Request if timezone.now() > request.expire_at: return self.create_request(request.email) # Create from the Old Request request = self.__reset_request_entity.insert_one({ "email": request.email, "expire_at": request.expire_at, "messages_count": request.messages_count + 1 }) return request.token if request is not False else False def create_request(self, email): request = self.__reset_request_entity.insert_one({ "email": email, "expire_after": self.__reset_expire_option, "messages_count": 0 }) return request.token if request is not False else False def send_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") user = self.__user_entity.get_one_by_email(email) return self.__task_core.delay( "forgot_password_email", { "app_name": app_name, "app_email": app_email, "app_url": app_url, "recipient_list": [email], "token": token, "subject": _("%s Password Reset") % (app_name), "template": "mails/reset_password.html", "fail_silently": False }, user.id)
class Profile(): def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__token = Token() self.__profile_entity = ProfileEntity() def get_profile(self, user_id): profile_data = { "first_name": "", "last_name": "", "username": "", "email": "", "job_title": "", "company": "", "address": "", "github_url": "", "twitter_url": "", "facebook_url": "", "access_token": "", "refresh_token": "", "avatar": "" } user = self.__user_entity.get_one_by_id(user_id) profile = self.__profile_entity.get_profile_by_user_id(user_id) if user is not False: profile_data["first_name"] = user.first_name profile_data["last_name"] = user.last_name profile_data["username"] = user.username profile_data["email"] = user.email profile_data["avatar"] = Gravatar(user.email).get_image() if profile is not False: profile_data["job_title"] = profile.job_title profile_data["company"] = profile.company profile_data["address"] = profile.address profile_data["github_url"] = profile.github_url profile_data["twitter_url"] = profile.twitter_url profile_data["facebook_url"] = profile.facebook_url profile_data["access_token"] = profile.access_token profile_data["refresh_token"] = profile.refresh_token return profile_data def update_profile(self, user_id, user_data): user_data["user"] = user_id if self.__profile_entity.profile_exists(user_data["user"]): status = self.__profile_entity.update_profile(user_data) status &= self.__user_entity.update_one_by_id(user_data["user"], user_data) return status else: status = (self.__profile_entity.create_profile(user_data) is not False) status &= self.__user_entity.update_one_by_id(user_data["user"], user_data) return status def update_access_token(self, user_id): token = self.__token.generate_token() while self.__profile_entity.token_used(token) is not False: token = self.__token.generate_token() return token if self.__profile_entity.update_access_token(user_id, token) else False def update_refresh_token(self, user_id): token = self.__token.generate_token() while self.__profile_entity.token_used(token) is not False: token = self.__token.generate_token() return token if self.__profile_entity.update_refresh_token(user_id, token) else False def get_profile_by_access_token(self, access_token): return self.__profile_entity.get_profile_by_access_token(access_token) def change_password(self, user_id, password): return self.__user_entity.update_password_by_user_id(user_id, password) def restore_session(self, user_id, request): return update_session_auth_hash(request, self.__user_entity.get_one_by_id(user_id)) def validate_password(self, user_id, password): return self.__user_entity.validate_password_by_user_id(user_id, password) def update_user(self, user_id, user_data): return self.__user_entity.update_one_by_id(self, user_id, user_data) def username_used_elsewhere(self, user_id, username): user = self.__user_entity.get_one_by_username(username) return False if user is False or user.id == user_id else True def email_used_elsewhere(self, user_id, email): user = self.__user_entity.get_one_by_email(email) return False if user is False or user.id == user_id else True