Skip to content

Commit

Permalink
Implement create capability
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Apr 18, 2024
1 parent 1b1ff99 commit 813c5e4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
8 changes: 6 additions & 2 deletions includes/event/event-capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wporg\TranslationEvents\Event;

use GP;
use WP_User;

class Event_Capabilities {
Expand All @@ -21,8 +22,11 @@ private function has_cap( string $cap, array $args, WP_User $user ): bool {
}

private function has_create( WP_User $user ): bool {
// TODO.
return true;
return $this->has_gp_crud( $user );
}

private function has_gp_crud( WP_User $user ): bool {
return apply_filters( 'gp_translation_events_can_crud_event', GP::$permission->user_can( $user, 'admin' ) );
}

public function register_hooks(): void {
Expand Down
2 changes: 1 addition & 1 deletion includes/event/event-form-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function handle( array $form_data ): void {
* @param bool $can_crud_event Whether the user can create, edit, or delete an event.
*/
$can_crud_event = apply_filters( 'gp_translation_events_can_crud_event', GP::$permission->current_user_can( 'admin' ) );
if ( 'create_event' === $action && ( ! $can_crud_event ) ) {
if ( 'create_event' === $action && ( ! current_user_can( 'create_translation_event' ) ) ) {
wp_send_json_error( esc_html__( 'The user does not have permission to create an event.', 'gp-translation-events' ), 403 );
}
if ( 'edit_event' === $action ) {
Expand Down
5 changes: 5 additions & 0 deletions includes/routes/event/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function handle(): void {
wp_safe_redirect( wp_login_url( home_url( $wp->request ) ) );
exit;
}

if ( ! current_user_can( 'create_translation_event' ) ) {
$this->die_with_error( 'You do not have permission to create events.' );
}

$event_page_title = 'Create Event';
$event_form_name = 'create_event';
$css_show_url = 'hide-event-url';
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<file>./wporg-gp-translation-events.php</file>

<rule ref="WordPress">
<properties>
<property name="custom_capabilities[]" value="create_translation_event"/>
</properties>
<exclude name="Squiz.Commenting.ClassComment.Missing"/>
<exclude name="Squiz.Commenting.FileComment.Missing"/>
<exclude name="Squiz.Commenting.FileComment.MissingPackageTag"/>
Expand Down
19 changes: 17 additions & 2 deletions tests/event/event-capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GP_UnitTestCase;
use Wporg\TranslationEvents\Attendee\Attendee_Repository;
use Wporg\TranslationEvents\Event\Event_Capabilities;
use Wporg\TranslationEvents\Event\Event_Repository;
use Wporg\TranslationEvents\Tests\Event_Factory;
use Wporg\TranslationEvents\Tests\Stats_Factory;
Expand All @@ -16,6 +15,22 @@ public function setUp(): void {
$this->stats_factory = new Stats_Factory();
$this->attendee_repository = new Attendee_Repository();
$this->event_repository = new Event_Repository( $this->attendee_repository );
$this->capilities = new Event_Capabilities();
}

public function test_cannot_create_if_no_crud_permission() {
$this->set_normal_user_as_current();

add_filter( 'gp_translation_events_can_crud_event', '__return_false' );

$this->assertFalse( current_user_can( 'create_translation_event' ) );
}

public function test_can_create_if_crud_permission() {
$this->set_normal_user_as_current();
get_current_user_id();

add_filter( 'gp_translation_events_can_crud_event', '__return_true' );

$this->assertTrue( current_user_can( 'create_translation_event' ) );
}
}

0 comments on commit 813c5e4

Please sign in to comment.