<commit_before><?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * User Class
 *
 * @package		Orion Project
 * @subpackage	Libraries
 * @category	Users
 * @author		<NAME>
 * @link		http://waldir.org/orion/
 */

/* 
 * This constant is used to make the login method
 * dont update the last login on database
 * This is only for optmization pruposes.
 *
 */


class CI_User_manager {
	
	protected $CI;

	function __construct(){
		$this->CI =& get_instance();
        
        // load session library
        $this->CI->load->library('session');
    }


    function save_user($full_name, $login, $password, $active = 1, $permissions=array()) {
        $hashed_password = <PASSWORD>($password);
        
        if( ! $this->login_exists($login) && $full_name!= "") {
            // This login is fine, proceed
            if ( $this->CI->db->insert('users', array('name'=>$full_name, 'login'=>$login, 'password'=>$<PASSWORD>, 'active'=>$active )) ) {
                
                // Saved successfully
                $new_user_id = $this->CI->db->insert_id();

                // Add the permissions
                $this->add_permission($new_user_id, $permissions);

                // Return the new user id
                return $new_user_id;
            }
        } else {
            return false;
        }
    }

    function delete_user($user_id){
        return $this->CI->db->delete('users', array('id'=>$user_id));
    }

    function login_exists($login_name){
        $exists = $this->CI->db->get_where('users', array('login'=>$login_name))->row();
        return sizeof($exists) != 0;
    }

    function user_has_permission($user_id, $permission_id){
        $result = $this->CI->db->get_where('users_permissions', array('user_id' => $user_id, 'permission_id' =>$permission_id));
        return ( $result->num_rows() == 1 );
    }

    function add_permission($user_id, $permissions) {
        // If array received we must call this recursively
        if(is_array($permissions)) {
            if(sizeof($permissions) != 0) {
                return FALSE;
            }
            // Foreach permission in the array call this function recursively
            foreach($permissions as $permission) {
                $this->add_permission($user_id, $permission);
            }
        } else {
            // Check if user already has this permission
            if( ! $this->user_has_permission($user_id, $permissions) ) {
                return $this->CI->db->insert('users_permissions', array('user_id'=>$user_id, 'permission_id'=>$permissions));
            } else {
                // User already has this permission
                return TRUE;
            }
        }
    }

    function save_permission($permission_name, $permission_description){
        $exists = $this->CI->db->get_where('permissions', array('name'=>$permission_name));
        if( $exists->num_rows() >= 1 ) {
            return $exists->row()->id;
        } else { 
            $insert = $this->CI->db->insert('permissions', array('name'=>$permission_name, 'description'=>$permission_description));
            if( $insert ) {
                return $this->CI->db->insert_id();
            } else {
                return FALSE;
            }
        }
    }

    function get_users_with_permission($permission_name){
        $permission = $this->CI->db->get_where('permissions', array('name'=>$permission_name))->row();
        if(sizeof($permission) == 0) {
            return FALSE;
        } else {<commit_msg>Some comments added to user_manager<commit_after><?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * User Class
 *
 * @package		Orion Project
 * @subpackage	Libraries
 * @category	Users
 * @author		<NAME>
 * @link		http://waldir.org/orion/
 */

/* 
 * This constant is used to make the login method
 * dont update the last login on database
 * This is only for optmization pruposes.
 *
 */


class CI_User_manager {
	
	protected $CI;

	function __construct(){
		$this->CI =& get_instance();
        
        // load session library
        $this->CI->load->library('session');
    }

    // Saves a new user
    function save_user($full_name, $login, $password, $active = 1, $permissions=array()) {
        $hashed_password = <PASSWORD>($password);
        
        if( ! $this->login_exists($login) && $full_name!= "") {
            // This login is fine, proceed
            if ( $this->CI->db->insert('users', array('name'=>$full_name, 'login'=>$login, 'password'=>$<PASSWORD>, 'active'=>$active )) ) {
                
                // Saved successfully
                $new_user_id = $this->CI->db->insert_id();

                // Add the permissions
                $this->add_permission($new_user_id, $permissions);

                // Return the new user id
                return $new_user_id;
            }
        } else {
            // Login already exists
            return false;
        }
    }
    
    // Delete the user
    function delete_user($user_id){
        return $this->CI->db->delete('users', array('id'=>$user_id));
    }

    // Check if there is already a login with that name
    function login_exists($login_name){
        $exists = $this->CI->db->get_where('users', array('login'=>$login_name))->row();
        return sizeof($exists) != 0;
    }

    // Checks if user already has the permission on database
    function user_has_permission($user_id, $permission_id){
        $result = $this->CI->db->get_where('users_permissions', array('user_id' => $user_id, 'permission_id' =>$permission_id));
        return ( $result->num_rows() == 1 );
    }

    // Links a permission with a user
    function add_permission($user_id, $permissions) {
        // If array received we must call this recursively
        if(is_array($permissions)) {
            if(sizeof($permissions) != 0) {
                return FALSE;
            }
            // Foreach permission in the array call this function recursively
            foreach($permissions as $permission) {
                $this->add_permission($user_id, $permission);
            }
        } else {
            // Check if user already has this permission
            if( ! $this->user_has_permission($user_id, $permissions) ) {
                return $this->CI->db->insert('users_permissions', array('user_id'=>$user_id, 'permission_id'=>$permissions));
            } else {
                // User already has this permission
                return TRUE;
            }
        }
    }

    // Creates a new permission
    function save_permission($permission_name, $permission_description){
        $exists = $this->CI->db->get_where('permissions', array('name'=>$permission_name));
        if( $exists->num_rows() >= 1 ) {
            return $exists->row()->id;
        } else { 
            $insert = $this->CI->db->insert('permissions', array('name'=>$permission_name, 'description'=>$permission_description));
            if( $insert ) {
                return $this->CI->db->insert_id();
            } else {
                return FALSE;
            }
        }
    }

    // Gets all users with a selected permission
    function get_users_with_permission($permission_name){
        $permission = $this->CI->db->get_where('permissions', array('name'=>$permission_name))->row();
        if(sizeof($permission) == 0) {
            return FALSE;
        } else {
{'fraction_non_alphanumeric': 0.14643337819650068, 'fraction_numerical': 0.0016150740242261105, 'mean_word_length': 2.1675191815856776, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 127, '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': '7233836', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2060, 'n_words': 466}