<commit_before><?php
/**
#  Copyright 2022 Firstwave (www.firstwave.com)
#
#  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.firstwave.com or email <EMAIL>
#
# *****************************************************************************
*
* PHP version 5.3.3
* 
* @category  Model
* @package   Roles
* @author    <NAME> <<EMAIL>>
* @copyright 2022 Firstwave
* @license   http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @version   GIT: Open-AudIT_4.3.2
* @link      http://www.open-audit.org
*/

/**
* Base Model Roles
*
* @access   public
* @category Model
* @package  Roles
* @author   <NAME> <<EMAIL>>
* @license  http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @link     http://www.open-audit.org
 */
class M_roles extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->log = new stdClass();
        $this->log->status = 'reading data';
        $this->log->type = 'system';
    }

    /**
     * Create an individual item in the database
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function create($data = null)
    {
        if (empty($data->ad_group) && ! empty($data->name)) {
            $data->ad_group = 'open-audit_roles_' . strtolower(str_replace(' ', '_', $data->name));
        }
        if ( ! empty($data->permissions) && gettype($data->permissions) === 'string') {
            // We likely have a CSV submitted item
            // Replace quotes as it should already be stringified JSON
            $item->permissions = str_replace("'", '"', $item->permissions);
        } else if ( ! empty($data->permissions) && gettype($data->permissions) !== 'string') {
            // We likely have a submitted form or a JSON submission using the API
            // Build up our permissions
            $permissions = new stdClass();
            foreach ($data->permissions as $endpoint => $object) {
                $permissions->{$endpoint} = '';
                foreach ($object as $key => $value) {
                    $permissions->{$endpoint} .= $key;
                }
            }
            $data->permissions = json_encode($permissions);
        }
        if ($id = $this->insert_collection('roles', $data)) {
            return intval($id);
        } else {
            return false;
        }
    }

    public function read($id = '')
    {
        $this->log->function = strtolower(__METHOD__);
        stdlog($this->log);
        if ($id == '') {
            $CI = & get_instance();
            $id = intval($CI->response->meta->id);
        } else {
            $id = intval($id);
        }
        $sql = "SELECT * FROM roles WHERE id = ?";
        $data = array($id);
        $result = $this->run_sql($sql, $data);
        if (!empty($result[0]->permissions)) {
            $result[0]->permissions = json_decode($result[0]->permissions);
        }
        $result = $this->format_data($result, 'roles');
        return ($result);
    }

    public function update()
    {
        $this->log->function = strtolower(__METHOD__);
        $this->log->status = 'updating data';
        stdlog($this->log);
        $CI = & get_instance();
        $sql = '';
        $fields = ' description name permissions ';
        foreach ($CI->response->meta->received_data->attributes as $key => $value) {
            if (strpos($fields, ' '.$key.' ') !== false) {
                if ($sql == '') {
                    $sql = "SET `" . $key . "` = '" . $value . "'";
                } else {
                    $sql .= ", `" . $key . "` = '" . $value . "'";
                }
            }
        }
        $sql = "UPDATE `roles` " . $sql . " WHERE id = " . intval($CI->response->meta->id);
        $this->run_sql($sql, array());
        return;
    }

    public function delete($id = '')
    {
        $data = array(intval($id));
        // Delete the role, only if not in default list
        $sql = "DELETE FROM `roles` WHERE id = ? AND name NOT IN ('admin', 'org_admin', 'reporter', 'user', 'collector')";
        $test = $this->run_sql($sql, $data);
        if ( ! empty($test)) {
            return true;
        } else {
            return false;
        }
    }

    public function read_sub_resource($id = '')
    {
        $this->log->function = strtolower(__METHOD__);
        stdlog($this->log);
        if ($id == '') {
            $CI = & get_instance();
            $id = intval($CI->response->meta->id);
        } else {
            $id = intval($id);
        }
        $sql = "SELECT name FROM `roles` WHERE id = ?";
        $data = array($id);
        $result = $this->run_sql($sql, $data);
        if (empty($result[0]->name)) {
            return false;<commit_msg>Allow to delete the reporter role in m_roles.<commit_after><?php
/**
#  Copyright 2022 Firstwave (www.firstwave.com)
#
#  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.firstwave.com or email <EMAIL>
#
# *****************************************************************************
*
* PHP version 5.3.3
* 
* @category  Model
* @package   Roles
* @author    <NAME> <<EMAIL>>
* @copyright 2022 Firstwave
* @license   http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @version   GIT: Open-AudIT_4.3.2
* @link      http://www.open-audit.org
*/

/**
* Base Model Roles
*
* @access   public
* @category Model
* @package  Roles
* @author   <NAME> <<EMAIL>>
* @license  http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @link     http://www.open-audit.org
 */
class M_roles extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->log = new stdClass();
        $this->log->status = 'reading data';
        $this->log->type = 'system';
    }

    /**
     * Create an individual item in the database
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function create($data = null)
    {
        if (empty($data->ad_group) && ! empty($data->name)) {
            $data->ad_group = 'open-audit_roles_' . strtolower(str_replace(' ', '_', $data->name));
        }
        if ( ! empty($data->permissions) && gettype($data->permissions) === 'string') {
            // We likely have a CSV submitted item
            // Replace quotes as it should already be stringified JSON
            $item->permissions = str_replace("'", '"', $item->permissions);
        } else if ( ! empty($data->permissions) && gettype($data->permissions) !== 'string') {
            // We likely have a submitted form or a JSON submission using the API
            // Build up our permissions
            $permissions = new stdClass();
            foreach ($data->permissions as $endpoint => $object) {
                $permissions->{$endpoint} = '';
                foreach ($object as $key => $value) {
                    $permissions->{$endpoint} .= $key;
                }
            }
            $data->permissions = json_encode($permissions);
        }
        if ($id = $this->insert_collection('roles', $data)) {
            return intval($id);
        } else {
            return false;
        }
    }

    public function read($id = '')
    {
        $this->log->function = strtolower(__METHOD__);
        stdlog($this->log);
        if ($id == '') {
            $CI = & get_instance();
            $id = intval($CI->response->meta->id);
        } else {
            $id = intval($id);
        }
        $sql = "SELECT * FROM roles WHERE id = ?";
        $data = array($id);
        $result = $this->run_sql($sql, $data);
        if (!empty($result[0]->permissions)) {
            $result[0]->permissions = json_decode($result[0]->permissions);
        }
        $result = $this->format_data($result, 'roles');
        return ($result);
    }

    public function update()
    {
        $this->log->function = strtolower(__METHOD__);
        $this->log->status = 'updating data';
        stdlog($this->log);
        $CI = & get_instance();
        $sql = '';
        $fields = ' description name permissions ';
        foreach ($CI->response->meta->received_data->attributes as $key => $value) {
            if (strpos($fields, ' '.$key.' ') !== false) {
                if ($sql == '') {
                    $sql = "SET `" . $key . "` = '" . $value . "'";
                } else {
                    $sql .= ", `" . $key . "` = '" . $value . "'";
                }
            }
        }
        $sql = "UPDATE `roles` " . $sql . " WHERE id = " . intval($CI->response->meta->id);
        $this->run_sql($sql, array());
        return;
    }

    public function delete($id = '')
    {
        $data = array(intval($id));
        // Delete the role, only if not in default list
        $sql = "DELETE FROM `roles` WHERE id = ? AND name NOT IN ('admin', 'org_admin', 'user', 'collector')";
        $test = $this->run_sql($sql, $data);
        if ( ! empty($test)) {
            return true;
        } else {
            return false;
        }
    }

    public function read_sub_resource($id = '')
    {
        $this->log->function = strtolower(__METHOD__);
        stdlog($this->log);
        if ($id == '') {
            $CI = & get_instance();
            $id = intval($CI->response->meta->id);
        } else {
            $id = intval($id);
        }
        $sql = "SELECT name FROM `roles` WHERE id = ?";
        $data = array($id);
        $result = $this->run_sql($sql, $data);
        if (empty($result[0]->name)) {
            return false;
{'fraction_non_alphanumeric': 0.16863528984190443, 'fraction_numerical': 0.004542976558240959, 'mean_word_length': 2.0626043405676127, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 14, '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': '122338', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3229, 'n_words': 881}