Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion app/Console/Commands/LdapSyncRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use App\Ldap\Committee;
use App\Ldap\Community;
use App\Ldap\Group;
use App\Ldap\Role;
use App\Models\GroupMembership;
use App\Models\RoleMembership;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Isolatable;
use Illuminate\Support\Facades\DB;

class LdapSyncRoles extends Command
{
Expand Down Expand Up @@ -45,6 +48,9 @@ public function handle()
->setDn(Community::$rootDn)
->search('ou', $this->argument('community'))
->get();

$this->comment("Committees:");

foreach ($realms as $realm){
$committees = Committee::fromCommunity($realm->getFirstAttribute('ou'))
->search('ou', $this->argument('committee'))
Expand All @@ -60,7 +66,7 @@ public function handle()
->where('committee_dn', $committee->getDn())
->where('role_cn', $role->getFirstAttribute('cn'))
->get();
$this->comment(" |-> " .$role->getDn());
$this->comment(" |-> " . $role->getDn());
// delete all members so far
$role->setAttribute('uniqueMember', ['']);
$ldapMembers = $role->members();
Expand All @@ -73,5 +79,38 @@ public function handle()
}
}
}

$this->comment("\nGroups:");

foreach ($realms as $realm) {
$groups = Group::fromCommunity($realm->getFirstAttribute('ou'))
->search('ou', $this->argument('group'))
->get();

foreach ($groups as $group) {
$this->comment("> " . $group->getDn());

// delete all members so far
$group->setAttribute('uniqueMember', ['']);

$roles = GroupMembership::where('group_dn', $group->getDn())->get();

foreach ($roles as $role) {
$roleCn = str_replace('cn=', '', substr($role->role_dn, 0, strpos($role->role_dn, ',')));
$committeeDn = strstr($role->role_dn, "ou=");
$activeMemberships = RoleMembership::active($date)
->where('committee_dn', $committeeDn)
->where('role_cn', $roleCn)
->get();

$ldapMembers = $group->users();
foreach ($activeMemberships as $membership) {
// add only active members back
$this->comment(" |-> $membership->username");
$ldapMembers->attach($membership->user->ldap());
}
}
}
}
}
}
60 changes: 60 additions & 0 deletions app/Console/Commands/MoveGroupRolesFromLdapToDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Console\Commands;

use App\Ldap\Community;
use App\Ldap\Group;
use App\Models\GroupMembership;
use Illuminate\Console\Command;

class MoveGroupRolesFromLdapToDatabase extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:move-group-roles-from-ldap-to-database
{community? : The short name to search for of the community}
{group? : The short name to search for of the group}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
$realms = Community::query()
->list() // only first level
->setDn(Community::$rootDn)
->search('ou', $this->argument('community'))
->get();

foreach ($realms as $realm) {
$groups = Group::fromCommunity($realm->getFirstAttribute('ou'))
->search('ou', $this->argument('group'))
->get();

foreach ($groups as $group) {
$this->comment("> " . $group->getDn());

// get roles
$roles = $group->members()->get();

foreach ($roles as $role) {
$this->comment($role);
GroupMembership::create([
'group_dn' => $group->getDn(),
'role_dn' => $role,
]);
}
}
}
}
}
15 changes: 15 additions & 0 deletions app/Livewire/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\Component;
use Mockery\Generator\StringManipulation\Pass\Pass;
Expand All @@ -18,6 +19,20 @@ class ChangePassword extends Component

public string $password_confirmation;

#[Locked]
public $currentUsername;

public function mount($username)
{
if ($username === auth()->user()->username || auth()->user()->can('superadmin', User::class)) {
$this->currentUsername = $username;
} elseif ($username === auth()->user()->username) {
$this->currentUsername = auth()->user()->username;
} else {
abort('403');
}
}

public function rules(): array
{
return [
Expand Down
11 changes: 8 additions & 3 deletions app/Livewire/Group/AddRoleToGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Livewire\Attributes\Locked;
use Livewire\Component;

use App\Models\GroupMembership;

class AddRoleToGroup extends Component
{
#[Locked]
Expand Down Expand Up @@ -42,9 +44,12 @@ public function render()

public function save()
{
/** @var Group $group */
$group = Group::findOrFail(Group::dnFrom($this->uid, $this->group_cn));
$group->members()->attach($this->selected_role_dn);
$group_dn = Group::findOrFail(Group::dnFrom($this->uid, $this->group_cn));
GroupMembership::create([
'group_dn' => $group_dn,
'role_dn' => $this->selected_role_dn,
]);

return redirect()->route('realms.groups.roles', ['uid' => $this->uid, 'cn' => $this->group_cn])
->with('message', __('groups.success_role_add'))
;
Expand Down
28 changes: 9 additions & 19 deletions app/Livewire/Group/ListRolesInGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Ldap\Community;
use App\Ldap\Group;
use App\Ldap\Role;
use App\Models\GroupMembership;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;
Expand Down Expand Up @@ -51,16 +52,15 @@ public function updatedSearch(): void
}

public function render() {
/** @var Group $group */
$group = Group::findOrFail($this->group_dn);
$roles = $group->members()->get();
$users = $group->users()->get();
// slice breaks it, whyever - get to go.
$rolesDB = GroupMembership::select('role_dn')->where('group_dn', $this->group_dn)->get();
$roles = [];
foreach ($rolesDB as $row) {
$role = Role::findOrFail($row->role_dn);
array_push($roles, $role);
}
return view(
'livewire.group.roles', [
'roles' => $roles,
'users' => $users,
'group' => $group,
]
)->title(__('groups.roles_list_title', ['name' => $this->group_cn]));
}
Expand All @@ -75,14 +75,7 @@ public function deletePrepare(string $role_dn): void
$committee = $role->committee();

$this->deleteRoleDN = $role_dn;
$this->deleteRoleName = [
'role_short' => $role->getFirstAttribute('description'),
'role_name' => $role->getFirstAttribute('cn'),
'committee_name' => $committee?->getFirstAttribute('description'),
'committee_short' => $committee?->getFirstAttribute('ou'),
'group_short' => $group->getFirstAttribute('cn'),
'group_name' => $group->getFirstAttribute('description')
];
$this->deleteRoleName = [ $role->getFirstAttribute('cn') ];

$this->showDeleteModal = true;
}
Expand All @@ -92,10 +85,7 @@ public function deleteCommit(): void
$community = Community::findByUid($this->realm_uid);
$this->authorize('delete', [Group::class, $community]);

$group = Group::findOrFail($this->group_dn);
$role = Role::findOrFail($this->deleteRoleDN);

$group->roles()->detach($role);
GroupMembership::where('group_dn', $this->group_dn)->where('role_dn', $this->deleteRoleDN)->delete();

$this->close();
}
Expand Down
17 changes: 11 additions & 6 deletions app/Livewire/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ class Profile extends Component
public $picture;
public $pictureUrl;

public function mount()
public $currentUsername;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vorsicht! Public probs können via client api frei verändert werden, auch wenn du da kein Input feld hin machst. Macht das hier Probleme? Evtl das [#Url] oder [#Locked] Prop verwenden stattdessen


public function mount($username)
{
$username = Auth::user()->username;
$user = User::findOrFailByUsername($username);
if ($username == auth()->user()->username || auth()->user()->can('superadmin', User::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.o.

$this->currentUsername = $username;
} elseif ($username == auth()->user()->username) {
$this->currentUsername = auth()->user()->username;
} else {
abort('403');
}
$user = User::findOrFailByUsername($this->currentUsername);
$this->uid = $user->getFirstAttribute('uid');
$this->givenName = $user->getFirstAttribute('givenName');
$this->sn = $user->getFirstAttribute('sn');
Expand All @@ -57,9 +65,6 @@ public function render()
public function save()
{
$this->validate();
if (Auth::user()->username !== $this->uid) {
abort('403');
}
$user = User::findOrFailByUsername($this->uid);
$user->setAttribute('mail', $this->email);
$user->setAttribute('givenName', $this->givenName);
Expand Down
74 changes: 74 additions & 0 deletions app/Livewire/Profile/Memberships.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Livewire\Profile;

use App\Ldap\User;
use App\Ldap\Role;
use App\Models\RoleMembership;
use Barryvdh\DomPDF\Facade\Pdf;
use Livewire\Attributes\Locked;
use Livewire\Component;

class Memberships extends Component
{
#[Locked]
public $currentUsername;

public bool $showOnlyActive = true;

public function mount($username)
{
if ($username == auth()->user()->username || auth()->user()->can('superadmin', User::class)) {
$this->currentUsername = $username;
} elseif ($username == auth()->user()->username) {
$this->currentUsername = auth()->user()->username;
} else {
abort('403');
}
}

public function getMemberships(string $username, bool $onlyActive)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public Methoden können bei livewire auch vom client aus aufgerufen werden. Wenn du das nicht machst (wie hier?) solltest du die private machen

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moderator:innen einer Studischaft haben keinen Zugriff auf den Profil-Bereich. Damit Moderator:innen trotzdem die Möglichkeit haben, die Tätigkeitsübersicht zu exportieren und es möglichst wenig Code-Dopplung gibt, greift auch der Members-Controller auf diese Funktion zu.

{
$query = RoleMembership::where('username', $username);
if ($onlyActive) {
$query->whereNull('until');
}
$roleMemberships = $query->get();
$memberships = [];
foreach ($roleMemberships as $row) {
$role = Role::findOrFail('cn=' . $row->role_cn . ',' . $row->committee_dn);
$memberships[] = [
'role' => $role,
'from' => $row->from,
'until' => $row->until,
'decided' => $row->decided,
'comment' => $row->comment,
];
}
return $memberships;
}

public function render()
{
$memberships = $this->getMemberships($this->currentUsername, $this->showOnlyActive);

return view('livewire.profile.memberships', [
'memberships' => $memberships,
])->title(__('Profile'));
}

public function exportPdf()
{
$memberships = $this->getMemberships($this->currentUsername, false);
$user = User::findOrFailByUsername($this->currentUsername);
$pdf = Pdf::loadView('pdfs.memberships', [
'fullName' => $user->cn[0],
'community' => null,
'memberships' => $memberships,
]);

return response()->streamDownload(function () use ($pdf) {
echo $pdf->stream();
}, strtolower(trans('profile.memberships')) . '_' . $this->currentUsername . '.pdf');
}
}
17 changes: 17 additions & 0 deletions app/Livewire/Realm/ListMembers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Ldap\Community;
use App\Ldap\User;
use Barryvdh\DomPDF\Facade\Pdf;
use Livewire\Attributes\Rule;
use Livewire\Attributes\Url;
use Livewire\Component;
Expand Down Expand Up @@ -87,4 +88,20 @@ public function close(): void
$this->showDeleteModal = false;
unset($this->deleteMemberName, $this->deleteMemberUsername);
}

public function exportPdf($username)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sehe ich das Richtig, das es einmal das PDF pro Person und das andere mal das PDF pro Rolle / Realm ist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falls du dich auf den Zugriff aus unterschiedlichen Komponenten auf diese Funktion beziehst, dann ist der einzige Unterschied, dass im Profil kein Realm mitgegeben werden kann, da die Nutzer nicht nur einem einzigen Realm zugeordnet sein können. Beim Export über die Mitglieder-Liste eines Realms kann der aktuelle Realm für den PDF-Export mitgegeben werden, sodass auch die Studischaft im Dokument angezeigt werden kann.

{
$memberships = app('App\Livewire\Profile\Memberships')->getMemberships($username, false);
$user = User::findOrFailByUsername($username);
$community = Community::findOrFailByUid($this->community_name);
$pdf = Pdf::loadView('pdfs.memberships', [
'fullName' => $user->cn[0],
'community' => $community->description[0],
'memberships' => $memberships,
]);

return response()->streamDownload(function () use ($pdf) {
echo $pdf->stream();
}, 'memberships-' . $username . '.pdf');;
}
}
Loading