<commit_before><?php namespace DMA\Friends\Classes;

use Auth;
use Event;
use Validator;
use Exception;
use ValidationException;
use RainLab\User\Models\Settings as UserSettings;
use RainLab\User\Models\User;
use DMA\Friends\Models\Usermeta;

/**
 * Manage custom authentication for friends
 *
 * @package DMA\Friends\Classes
 * @author <NAME>, <NAME>
 */
class AuthManager
{

    /**
     * Authenticate a user by either username, email, or member id
     *
     * @param array $data
     * An array of attributes to authenticate a user minimially requires the following
     * - login
     *      Provide either the email address or username to authenticate.  This 
     *      setting is configured in the administrative backend
     * - password
     *      <PASSWORD>
     * - no_password
     *      If true password authentication will be bypassed.  Use with caution
     *      as this can be a security breach if used incorrectly.
     *
     * @param array $rules
     * A set of validation rules to validate against
     * see http://laravel.com/docs/5.1/validation
     *
     * @return boolean
     * returns true if the user is authenticated
     */
    public static function auth($data, $rules = [])
    {
        $user = false;

        // Provide default rules
        $rules += [
            'login'     => 'required|between:4,64',
            'password'  => '<PASSWORD>',
        ];

        // Fire prelogin event before we start processing the user
        Event::fire('auth.prelogin', [$data, $rules]);

        if (!isset($data['no_password'])) {
            $data['no_password'] = false;
        }

        if (!$data['no_password']) {

            /*
             * Validate user credentials
             */
            $validation = Validator::make($data, $rules);
            if ($validation->fails()) {

                throw new ValidationException($validation);
            }

        }

        // Attempt to lookup by member_id
        if (!$user) {
            $user = self::isMember($data['login']);
        }

        // Attempt to look up barcode
        if (!$user) {
            $user = self::isBarcode($data['login']);
        }
   
        try {        

            if ($user && $data['no_password']) {
                Auth::login($user);
            } else {
                $user = self::loginUser($user, $data);
            }
        } catch(Exception $e) {
            $user = Event::fire('auth.invalidLogin', [$data, $rules]);
            if (!$user) throw new Exception($e);
        }

        if ($user) {
            Event::fire('auth.login', $user);
            return true;
        }

        return false;
    }

    /**
     * Lookup user by member id
     *
     * @param string $id
     * An id to lookup by member id
     *
     * @return User $user
     * A RainLab\User\Model\User object<commit_msg>return the user object instead of a boolean<commit_after><?php namespace DMA\Friends\Classes;

use Auth;
use Event;
use Validator;
use Exception;
use ValidationException;
use RainLab\User\Models\Settings as UserSettings;
use RainLab\User\Models\User;
use DMA\Friends\Models\Usermeta;

/**
 * Manage custom authentication for friends
 *
 * @package DMA\Friends\Classes
 * @author <NAME>, <NAME>
 */
class AuthManager
{

    /**
     * Authenticate a user by either username, email, or member id
     *
     * @param array $data
     * An array of attributes to authenticate a user minimially requires the following
     * - login
     *      Provide either the email address or username to authenticate.  This 
     *      setting is configured in the administrative backend
     * - password
     *      <PASSWORD>
     * - no_password
     *      If true password authentication will be bypassed.  Use with caution
     *      as this can be a security breach if used incorrectly.
     *
     * @param array $rules
     * A set of validation rules to validate against
     * see http://laravel.com/docs/5.1/validation
     *
     * @return User $user
     * returns the authenticated user object
     */
    public static function auth($data, $rules = [])
    {
        $user = false;

        // Provide default rules
        $rules += [
            'login'     => 'required|between:4,64',
            'password'  => '<PASSWORD>|min:4',
        ];

        // Fire prelogin event before we start processing the user
        Event::fire('auth.prelogin', [$data, $rules]);

        if (!isset($data['no_password'])) {
            $data['no_password'] = false;
        }

        if (!$data['no_password']) {

            /*
             * Validate user credentials
             */
            $validation = Validator::make($data, $rules);
            if ($validation->fails()) {

                throw new ValidationException($validation);
            }

        }

        // Attempt to lookup by member_id
        if (!$user) {
            $user = self::isMember($data['login']);
        }

        // Attempt to look up barcode
        if (!$user) {
            $user = self::isBarcode($data['login']);
        }
   
        try {        

            if ($user && $data['no_password']) {
                Auth::login($user);
            } else {
                $user = self::loginUser($user, $data);
            }
        } catch(Exception $e) {
            $user = Event::fire('auth.invalidLogin', [$data, $rules]);
            if (!$user) throw new Exception($e);
        }

        if ($user) {
            Event::fire('auth.login', $user);
            return $user;
        }

        return false;
    }

    /**
     * Lookup user by member id
     *
     * @param string $id
     * An id to lookup by member id
     *
     * @return User $user
     * A RainLab\User\Model\User object
{'fraction_non_alphanumeric': 0.11856745479833102, 'fraction_numerical': 0.0019123783031988874, 'mean_word_length': 1.7967914438502675, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5594471', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1556, 'n_words': 469}