Ejemplo n.º 1
0
    def post(self):
        (
            email,
            password,
            password_2,
            first_name,
            last_name,
        ) = self.get_arguments()

        try:
            email = auth.validate_email(email)
            auth.validate_password(password, password_2)
            password = auth.encrypt_password(password)
            persons_service.create_person(email, password, first_name,
                                          last_name)
            return {"registration_success": True}, 201
        except auth.PasswordsNoMatchException:
            return (
                {
                    "error": True,
                    "message": "Confirmation password doesn't match.",
                },
                400,
            )
        except auth.PasswordTooShortException:
            return {"error": True, "message": "Password is too short."}, 400
        except auth.EmailNotValidException as exception:
            return {"error": True, "message": str(exception)}, 400
Ejemplo n.º 2
0
Archivo: cli.py Proyecto: withgame/zou
def create_admin(email, password):
    "Create an admin user to allow usage of the API when database is empty."
    "Set password is 'default'."

    try:
        # Allow "*****@*****.**" to be invalid.
        if email != "*****@*****.**":
            auth.validate_email(email)
        password = auth.encrypt_password(password)
        persons_service.create_person(
            email, password, "Super", "Admin", role="admin"
        )
        print("Admin successfully created.")

    except IntegrityError:
        print("User already exists for this email.")
        sys.exit(1)

    except auth.PasswordsNoMatchException:
        print("Passwords don't match.")
        sys.exit(1)
    except auth.PasswordTooShortException:
        print("Password is too short.")
        sys.exit(1)
    except auth.EmailNotValidException:
        print("Email is not valid.")
        sys.exit(1)
Ejemplo n.º 3
0
    def test_create_person(self):
        person = persons_service.create_person(
            "*****@*****.**",
            auth.encrypt_password("passwordhash"),
            "John",
            "Doe",
        )
        person = persons_service.get_person_by_email(person["email"])
        self.assertEqual(person["first_name"], "John")

        person = persons_service.create_person(
            " [email protected] \n",
            auth.encrypt_password("passwordhash"),
            "John",
            "Doe",
        )
        person = persons_service.get_person_by_email("*****@*****.**")
        self.assertEqual(person["first_name"], "John")

        person = persons_service.create_person(
            " [email protected] \n",
            auth.encrypt_password("passwordhash"),
            "John",
            "Doe",
            departments=[None],
        )
        person = persons_service.get_person_by_email("*****@*****.**")
        self.assertEqual(person["first_name"], "John")
Ejemplo n.º 4
0
    def post(self):
        """
        Create a new user in the database. 
        ---
        tags:
        - Persons
        description: Set "default" as password.
                     User role can be set but only admins can create admin users.
        parameters:
          - in: body
            name: User
            description: Email, first and last name, phone, role, desktop login and department of user
            schema:
                type: object
                required:
                - email
                - first_name
                properties:
                    email:
                        type: string
                    first_name:
                        type: string  
                    last_name:
                        type: string
                    phone:
                        type: integer
                        example: 06 12 34 56 78
                    role:
                        type: string
                    desktop_login:
                        type: string
                    departments:
                        type: string
        responses:
            201:
                description: User created
        """
        permissions.check_admin_permissions()
        data = self.get_arguments()

        if persons_service.is_user_limit_reached():
            return {
                "error": True,
                "message": "User limit reached.",
                "limit": config.USER_LIMIT,
            }, 400
        else:
            person = persons_service.create_person(
                data["email"],
                auth.encrypt_password("default"),
                data["first_name"],
                data["last_name"],
                data["phone"],
                role=data["role"],
                desktop_login=data["desktop_login"],
                departments=data["departments"],
            )
        return person, 201
Ejemplo n.º 5
0
    def update_person_list_with_ldap_users(users):
        for user in users:
            first_name = user["first_name"]
            last_name = user["last_name"]
            desktop_login = user["desktop_login"]
            email = user["email"]
            active = user.get("active", True)
            if "thumbnail" in user and len(user["thumbnail"]) > 0:
                thumbnail = user["thumbnail"][0]
            else:
                thumbnail = ""

            person = None
            try:
                person = persons_service.get_person_by_desktop_login(
                    desktop_login)
            except PersonNotFoundException:
                try:
                    person = persons_service.get_person_by_email(email)
                except PersonNotFoundException:
                    pass

            if len(email) == 0 or email == "[]" or type(email) != str:
                email = "%s@%s" % (desktop_login, EMAIL_DOMAIN)

            if person is None and active is True:
                try:
                    person = persons_service.create_person(
                        email,
                        "default".encode("utf-8"),
                        first_name,
                        last_name,
                        desktop_login=desktop_login,
                    )
                    print("User %s created." % desktop_login)
                except:
                    print("User %s creation failed (email duplicated?)." %
                          (desktop_login))

            elif person is not None:
                try:
                    active = True
                    persons_service.update_person(
                        person["id"],
                        {
                            "email": email,
                            "first_name": first_name,
                            "last_name": last_name,
                            "active": active,
                        },
                    )
                    print("User %s updated." % desktop_login)
                except:
                    print("User %s update failed (email duplicated?)." %
                          (desktop_login))

            if person is not None and len(thumbnail) > 0:
                save_thumbnail(person, thumbnail)
Ejemplo n.º 6
0
 def test_create_person(self):
     person = persons_service.create_person(
         "*****@*****.**",
         auth.encrypt_password("passwordhash"),
         "John",
         "Doe"
     )
     person = persons_service.get_person_by_email(person["email"])
     self.assertEquals(person["first_name"], "John")
Ejemplo n.º 7
0
 def post(self):
     permissions.check_admin_permissions()
     data = self.get_arguments()
     person = persons_service.create_person(
         data["email"],
         auth.encrypt_password("default"),
         data["first_name"],
         data["last_name"],
         data["phone"],
         role=data["role"])
     return person, 201
Ejemplo n.º 8
0
def create_admin(email):
    "Create an admin user to allow usage of the API when database is empty."
    "Set password is 'default'."

    try:
        auth.validate_email(email)
        password = auth.encrypt_password("default")
        persons_service.create_person(email,
                                      password,
                                      "Super",
                                      "Admin",
                                      role="admin")
        print("Admin successfully created.")

    except auth.PasswordsNoMatchException:
        print("Passwords don't match.")
        sys.exit(1)
    except auth.PasswordTooShortException:
        print("Password is too short.")
        sys.exit(1)
    except auth.EmailNotValidException:
        print("Email is not valid.")
        sys.exit(1)
Ejemplo n.º 9
0
    def post(self):
        permissions.check_admin_permissions()
        data = self.get_arguments()

        if persons_service.is_user_limit_reached():
            return {
                "error": True,
                "message": "User limit reached.",
                "limit": config.USER_LIMIT,
            }, 400
        else:
            person = persons_service.create_person(
                data["email"],
                auth.encrypt_password("default"),
                data["first_name"],
                data["last_name"],
                data["phone"],
                role=data["role"],
                desktop_login=data["desktop_login"],
            )
        return person, 201
Ejemplo n.º 10
0
    def post(self):
        """
        Allow a user to register himself to the service.
        ---
        tags:
            - Authentification
        parameters:
          - in: body
            name: Credentials
            description: The email, password, confirmation password, first name and last name of the user
            schema:
                type: object
                required:
                - email
                - password
                - password_2
                - first_name
                - last_name
                properties:
                    email:
                        type: string
                    password:
                        type: string
                    password_2:
                        type: string
                    first_name:
                        type: string
                    last_name:
                        type: string
                    
        responses:
          201:
            description: Registration successful
          400:
            description: Invalid password or email
        """
        (
            email,
            password,
            password_2,
            first_name,
            last_name,
        ) = self.get_arguments()

        try:
            email = auth.validate_email(email)
            auth.validate_password(password, password_2)
            password = auth.encrypt_password(password)
            persons_service.create_person(email, password, first_name,
                                          last_name)
            return {"registration_success": True}, 201
        except auth.PasswordsNoMatchException:
            return (
                {
                    "error": True,
                    "message": "Confirmation password doesn't match.",
                },
                400,
            )
        except auth.PasswordTooShortException:
            return {"error": True, "message": "Password is too short."}, 400
        except auth.EmailNotValidException as exception:
            return {"error": True, "message": str(exception)}, 400