Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MWPermissionsLookup
91.67% covered (success)
91.67%
11 / 12
80.00% covered (warning)
80.00%
4 / 5
8.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 userHasRight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 userIsSitewideBlocked
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 userIsNamed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUser
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\MWEntity;
6
7use InvalidArgumentException;
8use MediaWiki\User\User;
9use MediaWiki\User\UserFactory;
10use MediaWiki\User\UserNameUtils;
11
12class MWPermissionsLookup implements IPermissionsLookup {
13
14    public const SERVICE_NAME = 'CampaignEventsPermissionLookup';
15
16    private UserFactory $userFactory;
17    private UserNameUtils $userNameUtils;
18
19    /**
20     * @param UserFactory $userFactory
21     * @param UserNameUtils $userNameUtils
22     */
23    public function __construct(
24        UserFactory $userFactory,
25        UserNameUtils $userNameUtils
26    ) {
27        $this->userFactory = $userFactory;
28        $this->userNameUtils = $userNameUtils;
29    }
30
31    /**
32     * @inheritDoc
33     */
34    public function userHasRight( string $username, string $right ): bool {
35        return $this->getUser( $username )->isAllowed( $right );
36    }
37
38    /**
39     * @inheritDoc
40     */
41    public function userIsSitewideBlocked( string $username ): bool {
42        $block = $this->getUser( $username )->getBlock();
43        return $block && $block->isSitewide();
44    }
45
46    /**
47     * @inheritDoc
48     */
49    public function userIsNamed( string $username ): bool {
50        return $this->getUser( $username )->isNamed();
51    }
52
53    /**
54     * @param string $username
55     * @return User
56     */
57    private function getUser( string $username ): User {
58        if ( $this->userNameUtils->isIP( $username ) ) {
59            return $this->userFactory->newAnonymous( $username );
60        }
61
62        $user = $this->userFactory->newFromName( $username );
63        if ( !$user ) {
64            throw new InvalidArgumentException( "'$username' is not a valid username." );
65        }
66        return $user;
67    }
68}