def user_sign_up():
    try:
        firstname = request.get_json()['firstname']
        lastname = request.get_json()['lastname']
        username = request.get_json()['username']
        email = request.get_json()['email']
        password = request.get_json()['password']
        confirm_pass = request.get_json()['confirm_password']
        is_admin = is_user_admin(username)

    except KeyError:
        abortFn("Please check your json keys and try again")

    # this 2 checks will throw an exception if none
    # of their requirements are met.
    check_password(password, confirm_pass)
    email = validate_email(email)

    user = UserModel(firstname=firstname,
                     username=username,
                     lastname=lastname,
                     email=email,
                     password=password,
                     is_admin=is_admin)

    # call the save_user method from the models
    user.save_user()
    return jsonify({
        "status": 201,
        "data": "User Registered Successfully!"
    }), 201
    def check_params(user):
        email = user.get("email")
        password = user.get("password")
        if (user is None or email is None or password is None):
            abort(make_response(jsonify(error=NULL_PARAMS), 400))

        if (not validate_email(user.get("email")) or password is None):
            abort(make_response(jsonify(error=INVALID_FORMAT), 400))
Example #3
0
    def create(cls,
               first_name,
               last_name,
               username=None,
               email=None,
               phone=None,
               email_otp=None,
               phone_otp=None):
        email_verified = False
        phone_verified = False
        phone_data = None
        if not email and not phone:
            raise_error('ERR-AUTH-DETAIL-MISSING')
        if email:
            if not validate_email(email):
                raise_error('ERR-GNRL-INVALID-EMAIL')
            if email_otp and (Email.get_email(email=email).otp != email_otp):
                raise_error('ERR-AUTH-INVALID-OTP')
            else:
                email_verified = True
            if UserProfile.match_user_from_email(email=email):
                raise_error('ERR-USER-OTHER-WITH-EMAIL')
        if phone:
            if not email:
                email = UserProfile.get_random_username_email(
                    first_name=first_name)[1]
            phone_data = validate_get_phone(phone)
            if phone_otp and (Phone.get_phone(
                    phone_number=phone_data['phone_number'],
                    phone_code=phone_data['phone_code']).otp != phone_otp):
                raise_error('ERR-AUTH-INVALID-OTP')
            else:
                phone_verified = True
            if UserProfile.match_user_from_phone(
                    phone_number=phone_data['phone_number'],
                    phone_code=phone_data['phone_code']):
                raise_error('ERR-USER-OTHER-WITH-PHONE')
        if username and UserProfile.match_user_from_username(username):
            raise_error('ERR-USER-OTHER-WITH-USERNAME')

        if username is None or not username:
            username = UserProfile.get_random_username(first_name)

        user = User.objects.create(username=username,
                                   email=email,
                                   first_name=first_name,
                                   last_name=last_name)
        user_profile = cls.objects.create(user=user)
        if email_verified:
            user_profile.email_verified = True
        if phone_data:
            user_profile.phone_number = phone_data['phone_number']
            user_profile.phone_code = phone_data['phone_code']
        if phone_verified:
            user_profile.phone_otp = phone_otp
            user_profile.phone_verified = True
        user_profile.save()
        return user_profile
Example #4
0
def reg_verify():
    name = request.args.get('name')
    value = request.args.get('value')

    if name == 'username':
        return validate_username(value)
    elif name == 'email':
        return validate_email(value)
    return 'Invalid Request', 400
Example #5
0
def reg_verify():
    name = request.args.get('name')
    value = request.args.get('value')

    if name == 'username':
        return validate_username(value)
    elif name == 'email':
        return validate_email(value)
    return 'Invalid Request', 400
Example #6
0
    def create(user,
               source,
               first_name=None,
               last_name='',
               phone=None,
               email=None):
        """
        Creates or Updates the contact and associated details like email, phone & name
        :param user:
        :param source:
        :param first_name:
        :param last_name:
        :param phone:
        :param email:
        :return:
        """
        if not validate_email(email) and not validate_phone(phone):
            return None
        contact_from_email = UserContact.get_from_email(user, email)
        contact_from_phone = UserContact.get_from_phone(user, phone)

        if not contact_from_email and not contact_from_phone:
            phone_data = get_phone_or_null(phone)
            contact = UserContact.objects.create(
                source=constants.imported_contact_sources[source],
                first_name=first_name,
                user=user,
                last_name=last_name)
            if email:
                contact = email.lower()
                contact.save()
            if phone_data:
                contact.phone_number = phone_data['phone_number']
                contact.phone_code = phone_data['phone_code']
                contact.save()

        else:
            contact = contact_from_phone or contact_from_email
            contact.first_name = first_name
            contact.last_name = last_name
            contact.source = source

            if not contact.email and email:
                contact.email = email

            if not contact.phone_number and phone:
                phone_data = get_phone_or_null(phone)
                if phone_data:
                    contact.phone_number = phone_data['phone_number']
                    contact.phone_code = phone_data['phone_code']
                    contact.save()

            contact.save()

        # contact.action_if_contact_on_tc(email=email, phone=phone)
        return contact
Example #7
0
 def validate_email(self):
     if self.email:
         try:
             self.email = validate_email(self.email)
         except ValueError as e:
             self.errors['critical']['email'] = e.args[0]
             self.email = None
     elif not self.email_address:
         self.errors['critical'][
             'email'] = 'An email was not provided during email creation.  This is required.'
Example #8
0
 def match_user_from_email(email):
     if not validate_email(email):
         raise_error('ERR-GNRL-002')
     try:
         query = User.objects.get(email__iexact=email).userprofile
         return query
     except User.MultipleObjectsReturned:
         raise_error('ERR-DJNG-003')
     except ObjectDoesNotExist:
         return None
Example #9
0
 def create(email, send_otp=False):
     if not validate_email(email):
         raise_error('ERR-GNRL-INVALID-EMAIL')
     try:
         obj = Email.objects.get(email=email)
     except ObjectDoesNotExist:
         obj = Email.objects.create(email=email,
                                    otp=random_with_N_digits(6))
     if send_otp and settings.ENV_SETUP == 'PRODUCTION':
         pass
     return obj
Example #10
0
    def register(cls, form):
        if not len(form['username']) > 2:
            return None, '用户名长度必须大于2'
        if not len(form['password']) > 2:
            return None, '密码太简单'
        if not len(form['email']) > 0 or not validate_email(form['email']):
            return None, '邮件格式不对'
        if cls.exist(username=form['username']):
            return None, '用户已经存在'
        if cls.exist(email=form['email']):
            return None, '邮件已经被使用'

        user = User.new(**form)
        return user, '注册成功,去登录吧'
Example #11
0
def validate_user(body: Optional[dict] = None):
    if not body or not body.__contains__('user'):
        raise HTTPException(400, detail="No user provided")

    if not body['user'].__contains__('email'):
        raise HTTPException(400, detail="No email provided")

    email = body['user']['email']

    if not utils.validate_email(email):
        raise HTTPException(400, detail="Invalid email")

    # dummy response
    headers = {"Content-type": "application/json"}
    return JSONResponse(content={"result": "success"}, headers=headers)
Example #12
0
 def post(self):
     args = self.parser.parse_args()
     fullnames = args.get("fullnames", "")
     username = args.get("username", "")
     email = args.get("email", "")
     password = args.get("password", "")
     confirm_pass = args.get("confirm_pass", "")
     if empty(fullnames) or empty(username) or empty(email) or empty(
             password) or empty(confirm_pass):
         return {"message": "All the fields are required"}, 400
     if not validate_fullname(fullnames):
         return {
             "message":
             "Please enter a valid full name"
             " it should contain first and last name which"
             " start with capital letters"
         }, 400
     if not validate_username(username):
         return {
             "message":
             "Please specify a valid username "
             " a username should contain lowercase"
             " letters only and be between 6 to 12 letters"
         }, 400
     if not validate_email(email):
         return {"message": "Please enter a valid email address"}, 400
     if not validate_password(password):
         return {
             "message":
             "Please enter a valid password"
             " it must contain atleast one"
             " lowercase, uppercase special character and a"
             " number"
         }, 400
     user = User(fullnames, username, password, email)
     if not check_password_hash(user.password, confirm_pass):
         return {
             "message": "The password and the verifications don't"
             " match"
         }, 400
     saved = user.save()
     if not saved:
         return {"message": "The username or email is already in use"}, 400
     return {
         "message": "You successfully signed up you can now login",
         "data": user.json1
     }
Example #13
0
    def update_email(cls, user, new_email, otp, password):
        if user is None or new_email is None or otp is None or password is None:
            return

        if not validate_email(email=new_email):
            raise_error('ERR-GNRL-IVALID-EMAIL')

        stored_otp = Email.get_email(email=new_email).otp
        if stored_otp != otp:
            raise_error('ERR-AUTH-INVALID-OTP')

        if not user.check_password(password):
            raise_error('ERR-AUTH-INVALID-PASSWORD')
        try:
            user_obj = User.objects.get(email=new_email)
            if user != user_obj:
                raise_error('ERR-USER-OTHER-WITH-EMAIL')
            else:
                raise_error('ERR-USER-YOU-WITH-EMAIL')
        except ObjectDoesNotExist:
            user.email = new_email
            user.save()
Example #14
0
 def validate_email(form, field):
     res = validate_email(field.data)
     if res == 'OK':
         return True
     else:
         raise ValidationError(res)
Example #15
0
 def validate_email(form, field):
     res = validate_email(field.data)
     if res == 'OK':
         return True
     else:
         raise ValidationError(res)