<commit_before><?php
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************

/**
 * @author <NAME> <<EMAIL>>
 *
 * 
 * @version 1.12.8
 *
 * @copyright Copyright (c) 2014, Opmantek
 * @license http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
 */
class M_oa_user extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_orgs($user_id)
    {
        $end = array();

        # only run if post 1.12.6
        if ($this->db->table_exists('oa_user_org')) {
            $sql = "SELECT * FROM oa_user_org WHERE user_id = ? ORDER BY access_level desc";
            $sql = $this->clean_sql($sql);
            $data = array(intval($user_id));
            $query = $this->db->query($sql, $data);
            $user_orgs = $query->result();
        }

        $sql = "SELECT * FROM oa_org";
        $sql = $this->clean_sql($sql);
        $query = $this->db->query($sql);
        $this->orgs = $query->result();

        # only run if post 1.12.6
        if ($this->db->table_exists('oa_user_org')) {
            foreach ($this->orgs as $org) {
                # need to allow for pre 1.12.6 DB structure
                if (!isset($org->id) and isset($org->org_id)) {
                    $org->id = $org->org_id;
                }
                # need to allow for pre 1.12.6 DB structure
                if (!isset($org->parent_id) and isset($org->org_parent_id)) {
                    $org->parent_id = $org->org_parent_id;
                }
                foreach ($user_orgs as $user_org) {
                    if ($user_org->org_id == $org->id) {
                        $end[$org->id] = $user_org->access_level;
                    }
                }
            }
        }
        # TODO - get a list of user_org.org_id's with perms like '%r%' and
        # feed it into the array below
        $org_id_list = array();
        foreach ($end as $key => $value) {
            $org_id_list[$key] = $value;
            foreach ($this->get_org($key, $value) as $key2 => $value2) {
                $org_id_list[$key2] = $value2;
            }
        }
        return($org_id_list);
    }


    private function get_org($org_id, $access_level)
    {
        $org_id_list = array();
        foreach ($this->orgs as $org) {
            if ($org->parent_id == $org_id and $org->id != 0) {
                $org_id_list[$org->id] = $access_level;
                #echo "Set OrgId: " . $org->id . " AccessLevel: $access_level\n-----\n";
                foreach ($this->get_org($org->id, $access_level) as $key2 => $value2) {
                    $org_id_list[$key2] = $value2;
                }
            }
        }
        return($org_id_list);
    }


    public function select_user($name)
    {
        $sql = "SELECT id FROM oa_user WHERE name = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($name);
        $query = $this->db->query($sql, $data);
        $row = $query->row();
        if ($query->num_rows() > 0) {
            $row = $query->row();
            return (intval($row->id));
        } else {
            return false;
        }
    }

    public function check_user_name($name, $id)
    {
        $sql = "SELECT id FROM oa_user WHERE name = ? AND id <> ?";
        $sql = $this->clean_sql($sql);
        $data = array($name, $id);
        $query = $this->db->query($sql, $data);
        $row = $query->row();
        if ($query->num_rows() > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function deactivate_user($id)
    {
        $sql = "DELETE FROM oa_group_user WHERE user_id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
        $sql = "UPDATE oa_user SET active = 'n' WHERE id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
    }

    public function activate_user($id)
    {
        $sql = "UPDATE oa_user SET active = 'y' WHERE id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
    }

    public function get_user_details($id)
    {
        $sql = "SELECT * FROM oa_user WHERE id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
        $result = $query->result();
        return ($result);
    }

    public function get_all_users()
    {
        $sql = "SELECT id, name, full_name, email, admin, active FROM oa_user ORDER BY name";
        $sql = $this->clean_sql($sql);
        $query = $this->db->query($sql);
        $result = $query->result();
        return ($result);
    }

    public function add_user($user)
    {
        if (isset($user->admin)) {
            if ($user->admin == 'on') {
                $user->admin = 'y';
            } else {
                $user->admin = 'n';
            }
        } else {
            $user->admin = 'n';
        }
        # create the password
        $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)); # get 256 random bits in hex
        $hash = hash("sha256", $salt.$user->password); # prepend the salt, then hash
        # store the salt and hash in the same string, so only 1 DB column is needed
        $encrypted_password = $salt.$hash;
        $sql = "INSERT INTO oa_user (name, full_name, email, password, theme, lang, admin, sam) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        $sql = $this->clean_sql($sql);
        $data = array("$user->name", "$user->full_name", "$user->email", "$encrypted_password", "$user->theme", "$user->lang", "$user->admin", "$user->sam", );
        $query = $this->db->query($sql, $data);
        return($this->db->insert_id());
    }

    public function edit_user($user)
    {
        // format the user_admin to 'y' or 'n'
        if (isset($user->admin)) {
            if ($user->admin == 'on') {
                $user->admin = 'y';
            } else {<commit_msg>Workaround for users not having a oa_user_org entry created.<commit_after><?php
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************

/**
 * @author <NAME> <<EMAIL>>
 *
 * 
 * @version 1.12.8
 *
 * @copyright Copyright (c) 2014, Opmantek
 * @license http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
 */
class M_oa_user extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_orgs($user_id)
    {
        $end = array();

        # only run if post 1.12.6
        if ($this->db->table_exists('oa_user_org')) {
            $sql = "SELECT * FROM oa_user_org WHERE user_id = ? ORDER BY access_level desc";
            $sql = $this->clean_sql($sql);
            $data = array(intval($user_id));
            $query = $this->db->query($sql, $data);
            $user_orgs = $query->result();
        }

        $sql = "SELECT * FROM oa_org";
        $sql = $this->clean_sql($sql);
        $query = $this->db->query($sql);
        $this->orgs = $query->result();

        # only run if post 1.12.6
        if ($this->db->table_exists('oa_user_org')) {
            foreach ($this->orgs as $org) {
                # need to allow for pre 1.12.6 DB structure
                if (!isset($org->id) and isset($org->org_id)) {
                    $org->id = $org->org_id;
                }
                # need to allow for pre 1.12.6 DB structure
                if (!isset($org->parent_id) and isset($org->org_parent_id)) {
                    $org->parent_id = $org->org_parent_id;
                }
                foreach ($user_orgs as $user_org) {
                    if ($user_org->org_id == $org->id) {
                        $end[$org->id] = $user_org->access_level;
                    }
                }
            }
        }
        # TODO - get a list of user_org.org_id's with perms like '%r%' and
        # feed it into the array below
        $org_id_list = array();
        foreach ($end as $key => $value) {
            $org_id_list[$key] = $value;
            foreach ($this->get_org($key, $value) as $key2 => $value2) {
                $org_id_list[$key2] = $value2;
            }
        }
        return($org_id_list);
    }


    private function get_org($org_id, $access_level)
    {
        $org_id_list = array();
        foreach ($this->orgs as $org) {
            if ($org->parent_id == $org_id and $org->id != 0) {
                $org_id_list[$org->id] = $access_level;
                #echo "Set OrgId: " . $org->id . " AccessLevel: $access_level\n-----\n";
                foreach ($this->get_org($org->id, $access_level) as $key2 => $value2) {
                    $org_id_list[$key2] = $value2;
                }
            }
        }
        return($org_id_list);
    }


    public function select_user($name)
    {
        $sql = "SELECT id FROM oa_user WHERE name = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($name);
        $query = $this->db->query($sql, $data);
        $row = $query->row();
        if ($query->num_rows() > 0) {
            $row = $query->row();
            return (intval($row->id));
        } else {
            return false;
        }
    }

    public function check_user_name($name, $id)
    {
        $sql = "SELECT id FROM oa_user WHERE name = ? AND id <> ?";
        $sql = $this->clean_sql($sql);
        $data = array($name, $id);
        $query = $this->db->query($sql, $data);
        $row = $query->row();
        if ($query->num_rows() > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function deactivate_user($id)
    {
        $sql = "DELETE FROM oa_group_user WHERE user_id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
        $sql = "UPDATE oa_user SET active = 'n' WHERE id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
    }

    public function activate_user($id)
    {
        $sql = "UPDATE oa_user SET active = 'y' WHERE id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
    }

    public function get_user_details($id)
    {
        $sql = "SELECT * FROM oa_user WHERE id = ? LIMIT 1";
        $sql = $this->clean_sql($sql);
        $data = array($id);
        $query = $this->db->query($sql, $data);
        $result = $query->result();
        return ($result);
    }

    public function get_all_users()
    {
        $sql = "SELECT id, name, full_name, email, admin, active FROM oa_user ORDER BY name";
        $sql = $this->clean_sql($sql);
        $query = $this->db->query($sql);
        $result = $query->result();
        return ($result);
    }

    public function add_user($user)
    {
        if (isset($user->admin)) {
            if ($user->admin == 'on') {
                $user->admin = 'y';
            } else {
                $user->admin = 'n';
            }
        } else {
            $user->admin = 'n';
        }
        # create the password
        $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)); # get 256 random bits in hex
        $hash = hash("sha256", $salt.$user->password); # prepend the salt, then hash
        # store the salt and hash in the same string, so only 1 DB column is needed
        $encrypted_password = $salt.$hash;
        $sql = "INSERT INTO oa_user (name, full_name, email, password, theme, lang, admin, sam) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        $sql = $this->clean_sql($sql);
        $data = array("$user->name", "$user->full_name", "$user->email", "$encrypted_password", "$user->theme", "$user->lang", "$user->admin", "$user->sam", );
        $query = $this->db->query($sql, $data);
        $id = $this->db->insert_id();
        // TODO - fix this for v2
        $sql = "INSERT INTO oa_user_org VALUES (NULL, $id, 0, 10, '')";
        $query = $this->db->query($sql);
        return($id);
    }

    public function edit_user($user)
    {
        // format the user_admin to 'y' or 'n'
        if (isset($user->admin)) {
            if ($user->admin == 'on') {
                $user->admin = 'y';
            } else {
{'fraction_non_alphanumeric': 0.16407068519941329, 'fraction_numerical': 0.00894042047915066, 'mean_word_length': 1.9650031062331745, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 197, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '6710619', 'n_tokens_mistral': 4657, 'n_tokens_neox': 4487, 'n_words': 1198}