def create_admin(email, password): "Create an admin user to allow usage of the API when database is empty." "Set password is 'default'." # If a user already exists, change its role to admin try: person = persons_service.get_person_by_email(email) if person['role'] != 'admin': persons_service.update_person(person['id'], {'role': 'admin'}) except PersonNotFoundException: 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)
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)
def save_thumbnail(person, thumbnail): from zou.app import app with app.app_context(): thumbnail_path = "/tmp/ldap_th.jpg" with open(thumbnail_path, "wb") as th_file: th_file.write(thumbnail) thumbnail_png_path = thumbnail_utils.convert_jpg_to_png( thumbnail_path) thumbnail_utils.turn_into_thumbnail( thumbnail_png_path, size=thumbnail_utils.BIG_SQUARE_SIZE) file_store.add_picture("thumbnails", person["id"], thumbnail_png_path) os.remove(thumbnail_png_path) persons_service.update_person(person["id"], {"has_avatar": True})
def test_add_logs(self): person = persons_service.get_person_by_email("*****@*****.**") person_raw = Person.get(person["id"]) person_raw.update({"email": "*****@*****.**"}) person = persons_service.get_person_by_email("*****@*****.**") person = persons_service.update_person( person["id"], {"email": "*****@*****.**"}) with pytest.raises(PersonNotFoundException): persons_service.get_person_by_email("*****@*****.**") person = persons_service.get_person_by_email("*****@*****.**")
def prepare_creation(self, instance_id): return persons_service.update_person(instance_id, {"has_avatar": True})
def test_get_active_persons(self): self.assertEqual(len(persons_service.get_persons()), 2) persons_service.update_person(self.person.id, {"active": False}) self.assertEqual(len(persons_service.get_active_persons()), 1)