<commit_before><?php

namespace luya\traits;

use Yii;
use yii\web\Response;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\QueryParamAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use yii\filters\Cors;
use yii\base\Model;
use luya\rest\UserBehaviorInterface;
use luya\web\filters\JsonCruftFilter;
use luya\helpers\RestHelper;

/**
 * Rest Behaviors Trait.
 *
 * This class overrides the default behaviors method of {{yii\rest\Controller}} controllers.
 *
 * The following changes are differ to the base implementation:
 *
 * + If {{luya\rest\UserBehaviorInterface}} is **not** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is removed.
 * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is enabled.
 * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `contentNegotiator` behavior ({{yii\filters\ContentNegotiator}}) is enabled.
 * + The `rateLimiter` behavior filter is **removed** by default.
 *
 * Read the {{luya\rest\UserBehaviorInterface}} about the configuration ability to protect the controller.
 *
 * @author <NAME> <<EMAIL>>
 * @since 1.0.0
 */
trait RestBehaviorsTrait
{
    /**
     * @var boolean Whether CORS should be enabled or not.
     */
    public $enableCors = false;
    
    /**
     * @var array An array with languages which are passed to {{yii\filters\ContentNegotiator::$languages}}. Example
     *
     * ```php
     * 'languages' => [
     *     'en',
     *     'de',
     * ],
     * ```
     * @since 1.0.7
     */
    public $languages = [];
    
    /**
     * @var boolean Whether a unparsable cruf should be added to the json response or not. When enabled you have to parse the json response first before interpreting
     * as json.
     * @since 1.0.7
     */
    public $jsonCruft = false;
    
    /**
     * Whether the rest controller is protected or not.
     *
     * @return boolean|\yii\web\User
     */
    private function getUserAuthClass()
    {
        if ($this instanceof UserBehaviorInterface) {
            $class = $this->userAuthClass();
            
            if (!$class) { // return false;
                return false;
            }
            
            if (!is_object($class)) {
                return Yii::createObject($class);
            }
    
            return $class;
        }
        
        return false;
    }
    
    /**
     * Return all Auth methods for Composite Auth.
     *
     * @return array
     * @since 1.0.21
     */
    public function getCompositeAuthMethods()
    {
        return [
            QueryParamAuth::class,
            HttpBearerAuth::class,
        ];   
    }

    /**
     * Override the default {{yii\rest\Controller::behaviors()}} method.
     * The following changes are differ to the base implementation:
     *
     * + If {{luya\rest\UserBehaviorInterface}} is **not** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is removed.
     * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is enabled.
     * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `contentNegotiator` behavior ({{yii\filters\ContentNegotiator}}) is enabled.
     * + The `rateLimiter` behavior filter is **removed** by default.
     *
     * @return array Returns an array with registered behavior filters based on the implementation type.
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        if (!$this->getUserAuthClass()) {
            unset($behaviors['authenticator']);
        } else {
            // change to admin user auth class
            $behaviors['authenticator'] = [
                'class' => CompositeAuth::class,
                'user' => $this->getUserAuthClass(),
                'authMethods' => $this->getCompositeAuthMethods(),
            ];
            
            if ($this->enableCors) {
                $behaviors['authenticator']['except'] = ['options'];
            }
        }
        
        if ($this->enableCors) {
            $behaviors['cors'] = Cors::class;
        }

        $behaviors['contentNegotiator'] = [
            'class' => ContentNegotiator::class,
            'formats' => [
                'application/json' => Response::FORMAT_JSON,
                'application/xml' => Response::FORMAT_XML,<commit_msg>add optional auth actions to rest behavior<commit_after><?php

namespace luya\traits;

use Yii;
use yii\web\Response;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\QueryParamAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use yii\filters\Cors;
use yii\base\Model;
use luya\rest\UserBehaviorInterface;
use luya\web\filters\JsonCruftFilter;
use luya\helpers\RestHelper;

/**
 * Rest Behaviors Trait.
 *
 * This class overrides the default behaviors method of {{yii\rest\Controller}} controllers.
 *
 * The following changes are differ to the base implementation:
 *
 * + If {{luya\rest\UserBehaviorInterface}} is **not** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is removed.
 * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is enabled.
 * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `contentNegotiator` behavior ({{yii\filters\ContentNegotiator}}) is enabled.
 * + The `rateLimiter` behavior filter is **removed** by default.
 *
 * Read the {{luya\rest\UserBehaviorInterface}} about the configuration ability to protect the controller.
 *
 * @author <NAME> <<EMAIL>>
 * @since 1.0.0
 */
trait RestBehaviorsTrait
{
    /**
     * @var boolean Whether CORS should be enabled or not.
     */
    public $enableCors = false;
    
    /**
     * @var array An array with languages which are passed to {{yii\filters\ContentNegotiator::$languages}}. Example
     *
     * ```php
     * 'languages' => [
     *     'en',
     *     'de',
     * ],
     * ```
     * @since 1.0.7
     * @see {{yii\filters\ContentNegotiator::$languages}}
     */
    public $languages = [];
    
    /**
     * @var boolean Whether a unparsable cruf should be added to the json response or not. When enabled you have to parse the json response first before interpreting
     * as json.
     * @since 1.0.7
     */
    public $jsonCruft = false;

    /**
     * @var array list of action IDs that this filter will be applied to, but auth failure will not lead to error.
     * It may be used for actions, that are allowed for public, but return some additional data for authenticated users.
     * Defaults to empty, meaning authentication is not optional for any action.
     * Since version 2.0.10 action IDs can be specified as wildcards, e.g. `site/*`.
     * @since 1.0.21
     * @see {{yii\filters\auth\AuthMethod::$optional}}
     */
    public $authOptional = [];
    
    /**
     * Whether the rest controller is protected or not.
     *
     * @return boolean|\yii\web\User
     */
    private function getUserAuthClass()
    {
        if ($this instanceof UserBehaviorInterface) {
            $class = $this->userAuthClass();
            
            if (!$class) { // return false;
                return false;
            }
            
            if (!is_object($class)) {
                return Yii::createObject($class);
            }
    
            return $class;
        }
        
        return false;
    }
    
    /**
     * Return all Auth methods for Composite Auth.
     *
     * @return array
     * @since 1.0.21
     */
    public function getCompositeAuthMethods()
    {
        return [
            QueryParamAuth::class,
            HttpBearerAuth::class,
        ];   
    }

    /**
     * Override the default {{yii\rest\Controller::behaviors()}} method.
     * The following changes are differ to the base implementation:
     *
     * + If {{luya\rest\UserBehaviorInterface}} is **not** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is removed.
     * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `authenticator` behavior ({{yii\filters\auth\CompositeAuth}}) is enabled.
     * + If {{luya\rest\UserBehaviorInterface}} **is** implemented, the `contentNegotiator` behavior ({{yii\filters\ContentNegotiator}}) is enabled.
     * + The `rateLimiter` behavior filter is **removed** by default.
     *
     * @return array Returns an array with registered behavior filters based on the implementation type.
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        if (!$this->getUserAuthClass()) {
            unset($behaviors['authenticator']);
        } else {
            // change to admin user auth class
            $behaviors['authenticator'] = [
                'class' => CompositeAuth::class,
                'user' => $this->getUserAuthClass(),
                'authMethods' => $this->getCompositeAuthMethods(),
                'optional' => $this->authOptional,
            ];
            
            if ($this->enableCors) {
                $behaviors['authenticator']['except'] = ['options'];
            }
        }
        
        if ($this->enableCors) {
            $behaviors['cors'] = Cors::class;
        }

        $behaviors['contentNegotiator'] = [
            'class' => ContentNegotiator::class,
            'formats' => [
                'application/json' => Response::FORMAT_JSON,
                'application/xml' => Response::FORMAT_XML,
{'fraction_non_alphanumeric': 0.12751608217472504, 'fraction_numerical': 0.0035277028429134675, 'mean_word_length': 2.3702797202797203, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4991822', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2730, 'n_words': 728}