-
Notifications
You must be signed in to change notification settings - Fork 16
RoleUpdateLogger: log changes in roles #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?php | ||
|
|
||
| namespace HMS\Entities; | ||
|
|
||
| use Carbon\Carbon; | ||
|
|
||
| class RoleUpdate | ||
| { | ||
| /** | ||
| * @var int | ||
| */ | ||
| protected $id; | ||
|
|
||
| /** | ||
| * @var User | ||
| */ | ||
| protected $user; | ||
|
|
||
| /** | ||
| * @var ?Role | ||
| */ | ||
| protected $roleAdded; | ||
|
|
||
| /** | ||
| * @var ?Role | ||
| */ | ||
| protected $roleRemoved; | ||
|
|
||
| /** | ||
| * @var Carbon | ||
| */ | ||
| protected $createdAt; | ||
|
|
||
| public function __construct(User $user, ?Role $roleAdded = null, ?Role $roleRemoved = null) | ||
| { | ||
| $this->user = $user; | ||
| $this->roleAdded = $roleAdded; | ||
| $this->roleRemoved = $roleRemoved; | ||
| $this->createdAt = Carbon::now(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the value of id. | ||
| * | ||
| * @return int | ||
| */ | ||
| public function getId(): int | ||
| { | ||
| return $this->id; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the value of user. | ||
| * | ||
| * @return User | ||
| */ | ||
| public function getUser(): User | ||
| { | ||
| return $this->user; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the value of roleAdded. | ||
| * | ||
| * @return ?Role | ||
| */ | ||
| public function getRoleAdded(): ?Role | ||
| { | ||
| return $this->roleAdded; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the value of roleRemoved. | ||
| * | ||
| * @return ?Role | ||
| */ | ||
| public function getRoleRemoved(): ?Role | ||
| { | ||
| return $this->roleRemoved; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the value of createdAt. | ||
| * | ||
| * @return Carbon | ||
| */ | ||
| public function getCreatedAt(): Carbon | ||
| { | ||
| return $this->createdAt; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # HMS.Entities.RoleUpdate.dcm.yml | ||
| HMS\Entities\RoleUpdate: | ||
| type: entity | ||
| repositoryClass: HMS\Repository\RoleUpdateRepository | ||
| table: role_updates | ||
| id: | ||
| id: | ||
| type: integer | ||
| options: | ||
| unsigned: true | ||
| generator: | ||
| strategy: AUTO | ||
| fields: | ||
| createdAt: | ||
| type: datetime | ||
| gedmo: | ||
| timestampable: | ||
| on: create | ||
| manyToOne: | ||
| user: | ||
| targetEntity: User | ||
| nullable: false | ||
| roleAdded: | ||
| targetEntity: Role | ||
| joinColumn: | ||
| name: added_role_id | ||
| roleRemoved: | ||
| targetEntity: Role | ||
| joinColumn: | ||
| name: removed_role_id |
39 changes: 39 additions & 0 deletions
39
app/HMS/Repositories/Doctrine/DoctrineRoleUpdateRepository.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| namespace HMS\Repositories\Doctrine; | ||
|
|
||
| use HMS\Entities\User; | ||
| use HMS\Entities\RoleUpdate; | ||
| use Doctrine\ORM\EntityRepository; | ||
| use HMS\Repositories\RoleUpdateRepository; | ||
|
|
||
| class DoctrineRoleUpdateRepository extends EntityRepository implements RoleUpdateRepository | ||
| { | ||
| /** | ||
| * @param $id | ||
| * @return array | ||
| */ | ||
| public function find($id) | ||
| { | ||
| return parent::find($id); | ||
| } | ||
|
|
||
| /** | ||
| * @param User $user | ||
| * @return array | ||
| */ | ||
| public function findByUser(User $user) | ||
| { | ||
| return parent::findByUser($user); | ||
| } | ||
|
|
||
| /** | ||
| * save RoleUpdate to the DB. | ||
| * @param RoleUpdate $roleUpdate | ||
| */ | ||
| public function save(RoleUpdate $roleUpdate) | ||
| { | ||
| $this->_em->persist($roleUpdate); | ||
| $this->_em->flush(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace HMS\Repositories; | ||
|
|
||
| use HMS\Entities\User; | ||
| use HMS\Entities\RoleUpdate; | ||
|
|
||
| interface RoleUpdateRepository | ||
| { | ||
| /** | ||
| * @param $id | ||
| * @return array | ||
| */ | ||
| public function find($id); | ||
|
|
||
| /** | ||
| * @param User $user | ||
| * @return array | ||
| */ | ||
| public function findByUser(User $user); | ||
|
|
||
| /** | ||
| * save RoleUpdate to the DB. | ||
| * @param RoleUpdate $roleUpdate | ||
| */ | ||
| public function save(RoleUpdate $roleUpdate); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| namespace App\Listeners; | ||
|
|
||
| use HMS\Entities\Role; | ||
| use HMS\Entities\RoleUpdate; | ||
| use App\Events\Roles\UserAddedToRole; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Illuminate\Queue\InteractsWithQueue; | ||
| use App\Events\Roles\UserRemovedFromRole; | ||
| use HMS\Repositories\RoleUpdateRepository; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
|
|
||
| class RoleUpdateLogger implements ShouldQueue | ||
| { | ||
| use InteractsWithQueue; | ||
|
|
||
| /** | ||
| * @var RoleUpdateRepository | ||
| */ | ||
| protected $roleUpdateRepository; | ||
|
|
||
| /** | ||
| * @var EntityManagerInterface | ||
| */ | ||
| protected $entityManager; | ||
|
|
||
| /** | ||
| * Create the event listener. | ||
| * | ||
| * @param RoleUpdateRepository $roleUpdateRepository | ||
| */ | ||
| public function __construct(RoleUpdateRepository $roleUpdateRepository, EntityManagerInterface $entityManager) | ||
| { | ||
| $this->roleUpdateRepository = $roleUpdateRepository; | ||
| $this->entityManager = $entityManager; | ||
| } | ||
|
|
||
| /** | ||
| * Handle user added to role events. | ||
| * | ||
| * @param UserAddedToRole $event | ||
| */ | ||
| public function onUserAddedToRole(UserAddedToRole $event) | ||
| { | ||
| if ($event->role->getName() == Role::MEMBER_APPROVAL) { | ||
| return; | ||
| } | ||
|
|
||
| $user = $this->entityManager->merge($event->user); | ||
| $role = $this->entityManager->merge($event->role); | ||
| $roleUpdate = new RoleUpdate($user, $role); | ||
| $this->roleUpdateRepository->save($roleUpdate); | ||
| } | ||
|
|
||
| /** | ||
| * Handle user removed from role events. | ||
| * | ||
| * @param UserRemovedFromRole $event | ||
| */ | ||
| public function onUserRemovedFromRole(UserRemovedFromRole $event) | ||
| { | ||
| $user = $this->entityManager->merge($event->user); | ||
| $role = $this->entityManager->merge($event->role); | ||
| $roleUpdate = new RoleUpdate($user, null, $role); | ||
| $this->roleUpdateRepository->save($roleUpdate); | ||
| } | ||
|
|
||
| /** | ||
| * Register the listeners for the subscriber. | ||
| * | ||
| * @param Illuminate\Events\Dispatcher $events | ||
| */ | ||
| public function subscribe($events) | ||
| { | ||
| $events->listen( | ||
| 'App\Events\Roles\UserAddedToRole', | ||
| 'App\Listeners\RoleUpdateLogger@onUserAddedToRole' | ||
| ); | ||
|
|
||
| $events->listen( | ||
| 'App\Events\Roles\UserRemovedFromRole', | ||
| 'App\Listeners\RoleUpdateLogger@onUserRemovedFromRole' | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing gets done before the log update entity gets persisted. What are the needs around atomicity?