<commit_before># UserBundle

User attributes, groups and roles management. Backend and SPA.

[![Latest Stable Version](https://poser.pugx.org/sokil/user-bundle/v/stable.png)](https://packagist.org/packages/sokil/user-bundle)
[![Total Downloads](http://img.shields.io/packagist/dt/sokil/user-bundle.svg)](https://packagist.org/packages/sokil/user-bundle)

## Installation

Install bundle through composer:
```
composer.phar require sokil/user-bundle
```

Add bundle to AppKernel:
```php
<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Sokil\UserBundle\UserBundle(),
        );
    }
}
```

## Configuration

Bundle declares some routes, so configure them manually or add pre-configured to your `./app.config/routing.yml`:
```yaml
user:
    resource: "@UserBundle/Resources/config/routing.yml"
    prefix:   /
```

Then set access control for some of them in `./app/config/security.yml`:
```yaml
security:
    # define encoder
    encoders:
        Sokil\UserBundle\Entity\User: sha512
    
    # add some roles
    role_hierarchy:
        ROLE_USER_VIEWER: [ROLE_USER]
        ROLE_USER_MANAGER: [ROLE_USER_VIEWER]
        ROLE_ADMIN:
          - ROLE_USER_MANAGER
        ROLE_SUPER_ADMIN:
          - ROLE_ADMIN
          - ROLE_ALLOWED_TO_SWITCH
    
    # define provider
    providers:
        user_provider_email:
            id: user.user_provider.email
            
    # configure filewall
    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # main zone
        main:
            pattern: ^/
            form_login:
                provider: user_provider_email
                # csrf_provider: form.csrf_provider
                success_handler: user.authentication_success_handler
                failure_handler: user.authentication_failure_handler
            logout:       true
            anonymous:    true
            remember_me:
                key: "%secret%"
                lifetime: 604800 # 1 week
                path: /
                name: token
                httponly: true
                
    # define access control
    access_control:
      - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
```

Create tables:
```
./app/console doctrine:schema:update
```


## User attributes

User attribures based on [EAV model](https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model). Attributes represented by entities, extended from class `UserAttribute`, values represented by entity `UserAttributeValue`. Attributes associayed with users' groups, so user has only attributes, related to it's groups.

## Authentication

There are two event listeners, which overrides response of login request to handle ajax requests:

```yaml<commit_msg>reg config<commit_after># UserBundle

User attributes, groups and roles management. Backend and SPA.

[![Latest Stable Version](https://poser.pugx.org/sokil/user-bundle/v/stable.png)](https://packagist.org/packages/sokil/user-bundle)
[![Total Downloads](http://img.shields.io/packagist/dt/sokil/user-bundle.svg)](https://packagist.org/packages/sokil/user-bundle)

## Installation

Install bundle through composer:
```
composer.phar require sokil/user-bundle
```

Add bundle to AppKernel:
```php
<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Sokil\UserBundle\UserBundle(),
        );
    }
}
```

## Configuration

Bundle declares some routes, so configure them manually or add pre-configured to your `./app.config/routing.yml`:
```yaml
user:
    resource: "@UserBundle/Resources/config/routing.yml"
    prefix:   /
```

Then set access control for some of them in `./app/config/security.yml`:
```yaml
security:
    # define encoder
    encoders:
        Sokil\UserBundle\Entity\User: sha512
    
    # add some roles
    role_hierarchy:
        ROLE_USER_VIEWER: [ROLE_USER]
        ROLE_USER_MANAGER: [ROLE_USER_VIEWER]
        ROLE_ADMIN:
          - ROLE_USER_MANAGER
        ROLE_SUPER_ADMIN:
          - ROLE_ADMIN
          - ROLE_ALLOWED_TO_SWITCH
    
    # define provider
    providers:
        user_provider_email:
            id: user.user_provider.email
            
    # configure filewall
    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # main zone
        main:
            pattern: ^/
            form_login:
                provider: user_provider_email
                # csrf_provider: form.csrf_provider
                success_handler: user.authentication_success_handler
                failure_handler: user.authentication_failure_handler
            logout:       true
            anonymous:    true
            remember_me:
                key: "%secret%"
                lifetime: 604800 # 1 week
                path: /
                name: token
                httponly: true
                
    # define access control
    access_control:
      - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
```
Add some bundle configuration to `app/config/config.yaml`:
```yaml
# User
user:
  registration:
    security:
      firewall: main
      roles:
        - ROLE_CLIENT
```

Create tables:
```
./app/console doctrine:schema:update
```


## User attributes

User attribures based on [EAV model](https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model). Attributes represented by entities, extended from class `UserAttribute`, values represented by entity `UserAttributeValue`. Attributes associayed with users' groups, so user has only attributes, related to it's groups.

## Authentication

There are two event listeners, which overrides response of login request to handle ajax requests:

```yaml
{'fraction_non_alphanumeric': 0.09607749722089884, 'fraction_numerical': 0.006352231221216452, 'mean_word_length': 2.5027808676307006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2904147', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1856, 'n_words': 453}