Harborn-digital / cookie-consent-bundle

Symfony bundle to append Cookie Consent to your website to comply to AVG/GDPR for cookies.
MIT License
19 stars 18 forks source link

Add remane cookie variable #33

Closed xorgxx closed 4 years ago

xorgxx commented 5 years ago

To be possible to modify "Cookie_Consent" and "Cookie_Category" by a variable in the config.yml

27

ch_cookie_consent:
    theme: 'dark' # light, dark
    categories: # Below are the default supported categories
        - 'analytics'
        - 'tracking'
        - 'marketing'
        - 'social_media'
    use_logger: false # Logs user actions to database

    # adding this in the config file <----------------------------
    cookie_prefix: 'neo_Consent'                                          # 'cookie_Consent'
    cookie_key_name: 'neo_Consent_Key'                                    # 'cookie_Consent_Key'
    cookie_consent_name: 'neo_Category_'                                  # 'cookie_Category_'
matthijsch commented 5 years ago

Isn't it sufficient to be able to change the prefix of the cookie names? In that case you only have to add the cookie_prefix option and that would leave the code much cleaner. Right now the cookiePrefix isn't used anywhere. Also I think only in the Enum class the custom prefix should be injected

xorgxx commented 5 years ago

Yes cookie_prefix did not see where to use, in first analyse. As you, maybe it's better to use only cookie_prefix and inject in Enum class but are making injection ? we cant modifie const, i see only one way : to create service CookieNameEnum with gutters and setters and inject this service where it needs. But this solution is almost the identical to me.

xorgxx commented 5 years ago
class CookieNameEnum
{
    const COOKIE_CONSENT_NAME           = 'cookie_Consent';
    const COOKIE_CONSENT_KEY_NAME       = 'cookie_Consent_Key';
    const COOKIE_CATEGORY_NAME_PREFIX   = 'cookie_Category_';

    private $cookiePrefix;

    /**
     * @var array
     */
    public static $prefixCookie = [
        "COOKIE_CONSENT_NAME"           => "",
        "COOKIE_CONSENT_KEY_NAME"       => "",
        "COOKIE_CATEGORY_NAME_PREFIX"   => "",
    ];

    private static $Prefix;

    public function __construct(string $cookiePrefix) 
    {
        self::$Prefix       = $cookiePrefix;
        self::$prefixCookie = [
            "COOKIE_CONSENT_NAME"           => self::$Prefix . self::COOKIE_CONSENT_NAME ,
            "COOKIE_CONSENT_KEY_NAME"       => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
            "COOKIE_CATEGORY_NAME_PREFIX"   => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
            ];
    }
    /**
     * Get all cookie consent prefix (this was only for test).
     */
    public static function getAvailableCookie ( ){

        self::$prefixCookie = [
            "COOKIE_CONSENT_NAME"           => self::$Prefix . self::COOKIE_CONSENT_NAME ,
            "COOKIE_CONSENT_KEY_NAME"       => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
            "COOKIE_CATEGORY_NAME_PREFIX"   => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
        ];

        return self::$prefixCookie;
    }

    /**
     * Get cookie category name.
     */
    public static function getCookieCategoryName(string $category): string
    {
        return self::$Prefix . self::COOKIE_CATEGORY_NAME_PREFIX.$category;
    }
}

and for exemple CookieChecker :

    /**
     * CookieChecker constructor.
     *
     * @param Request $request

     */
    public function __construct(Request $request, CookieNameEnum $cookieNameEnum) 
    {
        $this->request              = $request;
        $this->cookiePrefix         = $cookieNameEnum;
    }
.....
    /**
     * @return bool
     */
    public function isCookieConsentSavedByUser(  ): bool
    {
        return $this->request->cookies->has( CookieNameEnum::$prefixCookie["COOKIE_CONSENT_NAME"]);
    }
reyostallenberg commented 5 years ago

Also:

xorgxx commented 5 years ago

in the last version i have include :

class CookieNameEnum
{
    const COOKIE_PREFIX                 = 'cookie_';
    const COOKIE_CONSENT_NAME           = 'Consent';
    const COOKIE_CONSENT_KEY_NAME       = '_Key';
    const COOKIE_CATEGORY_NAME_PREFIX   = 'Category_';

    private $cookiePrefix;

    /**
     * @var array
     */
    public static $prefixCookie = [
        "COOKIE_CONSENT_NAME"           => "",
        "COOKIE_CONSENT_KEY_NAME"       => "",
        "COOKIE_CATEGORY_NAME_PREFIX"   => "",
    ];

    private static $Prefix;
    private static $Consent_name;
    private static $Consent_key;
    private static $Consent_category;

    public function __construct(string $cookiePrefix, string $cookieKeyName, string $cookieConsentName, string $cookieConsentCategory) //, string $cookiePrefix
    {
        self::$Prefix               = $cookiePrefix;
        self::$Consent_name         = $cookieConsentName;
        self::$Consent_key          = $cookieKeyName;
        self::$Consent_category     = $cookieConsentCategory;

        self::$prefixCookie = [
            "COOKIE_CONSENT_NAME"           => self::$Prefix . self::$Consent_name ,
            "COOKIE_CONSENT_KEY_NAME"       => self::$Prefix . self::$Consent_name . self::$Consent_key ,
            "COOKIE_CATEGORY_NAME_PREFIX"   => self::$Prefix . self::$Consent_category ,
            ];
    }
    /**
     * Get all cookie consent prefix (this was only for test).
     */
    public static function getAvailableCookie ( ){

        self::$prefixCookie = [
            "COOKIE_CONSENT_NAME"           => self::$Prefix . self::$Consent_name ,
            "COOKIE_CONSENT_KEY_NAME"       => self::$Prefix . self::$Consent_name . self::$Consent_key ,
            "COOKIE_CATEGORY_NAME_PREFIX"   => self::$Prefix . self::$Consent_category ,
        ];

        return self::$prefixCookie;
    }

    /**
     * Get cookie category name.
     */
    public static function getCookieCategoryName(string $category): string
    {
        return self::$Prefix . self::$Consent_category.$category;
    }
}

i think that it can be gainful and it's include. it das not take must.