Skip to content

Commit

Permalink
Add admin action to create user in LDAP
Browse files Browse the repository at this point in the history
  • Loading branch information
txels committed Apr 9, 2024
1 parent 338df60 commit d9a955c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
11 changes: 8 additions & 3 deletions create_test_ldap_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class User:
username: str
first_name: str
last_name: str
email: str


if __name__ == "__main__":
Expand All @@ -19,15 +20,19 @@ class User:
username = sys.argv[1]
password = sys.argv[2]

user = User(username=username, first_name=username, last_name="Pirata")
email = f"{username}@pirata.cat"
user = User(
username=username,
first_name=username,
last_name="Pirata",
email=f"{username}@pirata.cat",
)

settings.configure()
from humans import directory

conn = directory.connect()
ou, ou_attrs = directory.create_ou(conn, "afiliats")
user_dn, user_attrs = directory.create_user(conn, user, email)
user_dn, user_attrs = directory.create_user(conn, user)
directory.set_password(conn, user_dn, password)

print(f"{user_dn=}\n{password=}\n{user_attrs}")
5 changes: 3 additions & 2 deletions humans/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ def connect() -> LDAPObject:
return connection


def create_user(connection, user, email):
def create_user(connection, user):
name = user.username.encode("utf-8")
user_dn = f"uid={user.username},{ORG_UNIT}"

user_attrs = {}
user_attrs["objectClass"] = [b"pilotPerson"]
user_attrs["cn"] = (user.first_name.encode("utf-8"),)
user_attrs["sn"] = (user.last_name.encode("utf-8"),)
user_attrs["mail"] = (email.encode("utf-8"),)
user_attrs["sn"] = (name,)
user_attrs["uid"] = (name.lower(),)
if user.email:
user_attrs["mail"] = (user.email.encode("utf-8"),)

user_ldif = modlist.addModlist(user_attrs)
result = connection.add_s(user_dn, user_ldif)
Expand Down
2 changes: 1 addition & 1 deletion humans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def save(self, *args, **kwargs):

def __ldap__save(self):
connection = directory.connect()
directory.create_user(connection, self, self.email)
directory.create_user(connection, self)
return

def __ldap__set_password(self, password):
Expand Down
14 changes: 12 additions & 2 deletions shipanaro/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,29 @@ class ShipanaroGroupAdmin(GroupAdmin, ShipanaroModelAdmin):
admin.site.register(Group, ShipanaroGroupAdmin)


@admin.action(description=_("Create user in LDAP"))
def create_ldap_user(modeladmin, request, queryset):
from humans import directory

conn = directory.connect()

for member in queryset:
directory.create_user(conn, member.user)


@admin.action(description=_("Send password reset email"))
def send_password_reset(modeladmin, request, queryset):
for member in queryset:
send_reset_password_email(member.user.email)


class MembershipAdmin(ShipanaroModelAdmin):
actions = [send_password_reset]
actions = [send_password_reset, create_ldap_user]
list_display = (
"uid",
"activated",
"user__last_name",
"user__first_name",
"user__last_name",
"user__username",
"user__email",
"birthday",
Expand Down

0 comments on commit d9a955c

Please sign in to comment.