<commit_before><?php
/**
 * Created by PhpStorm.
 * User: p2
 * Date: 8/21/14
 * Time: 3:46 PM
 */

namespace Main\Service;


use Main\Context\Context,
    Main\DataModel\Image,
    Main\DB,
    Main\Exception\Service\ServiceException,
    Main\Helper\ArrayHelper,
    Main\Helper\MongoHelper,
    Main\Helper\ResponseHelper,
    Main\Helper\URL,
    Main\Helper\UserHelper,
    Main\Http\RequestInfo,
    Valitron\Validator;

class UserService extends BaseService {
    protected $fields = ["type", "display_name", "username", "email", "password", "gender", "birth_date", "picture", "mobile", "website", "fb_id", "fb_name", "type", "detail"];

    public function getCollection(){
        $db = DB::getDB();
        return $db->users;
    }
    
    public function getEventCollection(){
        $db = DB::getDB();
        return $db->event;
    }
    
    public function getSnifferCollection(){
        $db = DB::getDB();
        return $db->sniffer;
    }
    
    public function getGalleryCollection(){
        $db = DB::getDB();
        return $db->gallery;
    }

    public function add($params, Context $ctx){
        $allow = ["username", "email", "password", "gender", "birth_date"];
        $data = ArrayHelper::filterKey($allow, $params);
        
        $v = new Validator($data);
        $v->rule('required', ["username", "email", "password"]);
        $v->rule('email', ["email"]);
        $v->rule('lengthBetween', 'username', 4, 32);
        $v->rule('lengthBetween', 'password', 6, 32);

        if(!$v->validate()) {
            throw new ServiceException(ResponseHelper::validateError($v->errors()));
        }
        
        // Check username and email are duplicate?
        $user_count = $this->getCollection()->find([
            '$or' => [
                ['username' => $params['username']],
                ['email' => $params['email']]
            ]
        ])->count();
        if($user_count > 0){
            throw new ServiceException(ResponseHelper::validateError(['username'=> ['Duplicate username'], 'email'=> ['Duplicate email']]));
        }
        
        $now = new \MongoDate();
        $default_setting = UserHelper::defaultSetting();
        $birth_date = new \MongoDate(strtotime($data['birth_date'].' 00:00:00'));
        
        $id = new \MongoId();
        $user_private_key = UserHelper::generate_key();
        $access_token = UserHelper::generate_token(MongoHelper::standardId($id), $user_private_key);

        $entity = [
            '_id' => $id,

            'password' => UserHelper::generate_password($data['password'], $user_private_key),
            'display_name' => $data['username'],
            'birth_date' => $birth_date,
            'email' => $data['email'],
            'username' => $data['username'],

            'display_notification_number' => 0,
            'type' => 'normal',

            'setting' => $default_setting,
            'created_at' => $now,
            'last_login' => $now,

            'access_token' => $access_token,
            'private_key' => $user_private_key,
            'level' => 1,
            'advertiser' => 0,

            'group_role' => ['group_id' => '54e855072667467f7709320e', 'role_perm_id' => '54eaf79810f0ed0d0a8b4568']
        ];
        
        // If send from guest
        $token = RequestInfo::getToken();
        if($token === false){
            $this->getCollection()->insert($entity);
        }else{
            unset($entity['_id']);
            unset($entity['private_key']);
            unset($entity['created_at']);
            
            $token_count = $this->getCollection()->findOne(['access_token' => $token],['_id','private_key','access_token','type']);
            if ($token_count === null) {
                throw new ServiceException(ResponseHelper::error('Invalid token'));
            }
            
            // Override access_token<commit_msg>Update add field gender<commit_after><?php
/**
 * Created by PhpStorm.
 * User: p2
 * Date: 8/21/14
 * Time: 3:46 PM
 */

namespace Main\Service;


use Main\Context\Context,
    Main\DataModel\Image,
    Main\DB,
    Main\Exception\Service\ServiceException,
    Main\Helper\ArrayHelper,
    Main\Helper\MongoHelper,
    Main\Helper\ResponseHelper,
    Main\Helper\URL,
    Main\Helper\UserHelper,
    Main\Http\RequestInfo,
    Valitron\Validator;

class UserService extends BaseService {
    protected $fields = ["type", "display_name", "username", "email", "password", "gender", "birth_date", "picture", "mobile", "website", "fb_id", "fb_name", "type", "detail"];

    public function getCollection(){
        $db = DB::getDB();
        return $db->users;
    }
    
    public function getEventCollection(){
        $db = DB::getDB();
        return $db->event;
    }
    
    public function getSnifferCollection(){
        $db = DB::getDB();
        return $db->sniffer;
    }
    
    public function getGalleryCollection(){
        $db = DB::getDB();
        return $db->gallery;
    }

    public function add($params, Context $ctx){
        $allow = ["username", "email", "password", "gender", "birth_date"];
        $data = ArrayHelper::filterKey($allow, $params);
        
        $v = new Validator($data);
        $v->rule('required', ["username", "email", "password"]);
        $v->rule('email', ["email"]);
        $v->rule('lengthBetween', 'username', 4, 32);
        $v->rule('lengthBetween', 'password', 6, 32);

        if(!$v->validate()) {
            throw new ServiceException(ResponseHelper::validateError($v->errors()));
        }
        
        // Check username and email are duplicate?
        $user_count = $this->getCollection()->find([
            '$or' => [
                ['username' => $params['username']],
                ['email' => $params['email']]
            ]
        ])->count();
        if($user_count > 0){
            throw new ServiceException(ResponseHelper::validateError(['username'=> ['Duplicate username'], 'email'=> ['Duplicate email']]));
        }
        
        $now = new \MongoDate();
        $default_setting = UserHelper::defaultSetting();
        $birth_date = new \MongoDate(strtotime($data['birth_date'].' 00:00:00'));
        
        $id = new \MongoId();
        $user_private_key = UserHelper::generate_key();
        $access_token = UserHelper::generate_token(MongoHelper::standardId($id), $user_private_key);

        $entity = [
            '_id' => $id,

            'password' => UserHelper::generate_password($data['password'], $user_private_key),
            'display_name' => $data['username'],
            'birth_date' => $birth_date,
            'email' => $data['email'],
            'username' => $data['username'],
            'gender' => $data['gender'],

            'display_notification_number' => 0,
            'type' => 'normal',

            'setting' => $default_setting,
            'created_at' => $now,
            'last_login' => $now,

            'access_token' => $access_token,
            'private_key' => $user_private_key,
            'level' => 1,
            'advertiser' => 0,

            'group_role' => ['group_id' => '54e855072667467f7709320e', 'role_perm_id' => '54eaf79810f0ed0d0a8b4568']
        ];
        
        // If send from guest
        $token = RequestInfo::getToken();
        if($token === false){
            $this->getCollection()->insert($entity);
        }else{
            unset($entity['_id']);
            unset($entity['private_key']);
            unset($entity['created_at']);
            
            $token_count = $this->getCollection()->findOne(['access_token' => $token],['_id','private_key','access_token','type']);
            if ($token_count === null) {
                throw new ServiceException(ResponseHelper::error('Invalid token'));
            }
            
            // Override access_token
{'fraction_non_alphanumeric': 0.17038643030225736, 'fraction_numerical': 0.015559239892870808, 'mean_word_length': 2.324289953370072, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 88, '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': '1201130', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2262, 'n_words': 426}