Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
DAOAccessControl
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 wrap
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getDAO
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 escapeForWikitext
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 escapeForHtml
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 userCanAccess
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * (c) Aaron Schulz 2013, GPL
4 *
5 * @license GPL-2.0-or-later
6 */
7
8namespace MediaWiki\Extension\OAuth\Control;
9
10use LogicException;
11use MediaWiki\Context\ContextSource;
12use MediaWiki\Context\IContextSource;
13use MediaWiki\Extension\OAuth\Backend\MWOAuthDAO;
14use MediaWiki\Message\Message;
15
16/**
17 * Wrapper of an MWOAuthDAO that handles authorization to view fields
18 */
19class DAOAccessControl extends ContextSource {
20    /** @var MWOAuthDAO */
21    protected $dao;
22
23    /**
24     * @param MWOAuthDAO $dao
25     * @param IContextSource $context
26     */
27    final protected function __construct( MWOAuthDAO $dao, IContextSource $context ) {
28        $this->dao = $dao;
29        $this->setContext( $context );
30    }
31
32    /**
33     * @param MWOAuthDAO|false|null $dao
34     * @param IContextSource $context
35     * @return static|null|false
36     */
37    final public static function wrap( $dao, IContextSource $context ) {
38        if ( $dao instanceof MWOAuthDAO ) {
39            return new static( $dao, $context );
40        } elseif ( $dao === null || $dao === false ) {
41            return $dao;
42        } else {
43            throw new LogicException( "Expected MWOAuthDAO object, null, or false." );
44        }
45    }
46
47    /**
48     * @return MWOAuthDAO
49     */
50    public function getDAO() {
51        return $this->dao;
52    }
53
54    /**
55     * Helper to make return value of get() safe for wikitext
56     *
57     * @param Message|string $value
58     * @return string For use in wikitext
59     * @param-taint $value escapes_escaped
60     */
61    final public function escapeForWikitext( $value ) {
62        if ( $value instanceof Message ) {
63            return wfEscapeWikiText( $value->plain() );
64        } else {
65            return wfEscapeWikiText( $value );
66        }
67    }
68
69    /**
70     * Helper to make return value of get() safe for HTML
71     *
72     * @param Message|string $value
73     * @return string HTML escaped
74     * @param-taint $value escapes_escaped
75     */
76    final public function escapeForHtml( $value ) {
77        if ( $value instanceof Message ) {
78            return $value->parse();
79        } else {
80            return htmlspecialchars( $value );
81        }
82    }
83
84    /**
85     * Get the value of a field, taking into account user permissions.
86     * An appropriate Message will be returned if access is denied.
87     *
88     * @param string $name
89     * @param callback|null $sCallback Optional callback to apply to result on access success
90     * @return mixed Returns a Message on access failure
91     */
92    final public function get( $name, $sCallback = null ) {
93        $msg = $this->dao->userCanAccess( $name, $this->getContext() );
94        if ( $msg !== true ) {
95            // should be a Message object
96            return $msg;
97        } else {
98            $value = $this->dao->get( $name );
99            return $sCallback ? $sCallback( $value ) : $value;
100        }
101    }
102
103    /**
104     * Check whether the user can access the given field(s).
105     * @param string|array $names A field name or a list of names.
106     * @return bool
107     */
108    final public function userCanAccess( $names ) {
109        foreach ( (array)$names as $name ) {
110            if ( !$this->dao->userCanAccess( $name, $this->getContext() ) ) {
111                return false;
112            }
113        }
114        return true;
115    }
116}