<commit_before><?php
/**
 * Yet another blogging system.
 *
 * @license Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0.txt)
 * @author <NAME> <<EMAIL>>
 */

namespace Beam\Auth;

class User extends \Rapid\Auth\User
{
	const ROLE_VIEWER = 1;
	const ROLE_EDITOR = 2;
	const ROLE_ADMIN  = 4;

	/**
	 * Login hook.
	 */
	public function on_login() {
		global $USER;

		$USER->roles = $this->get_roles($USER->id);
	}

	/**
	 * Get roles for a user.
	 */
	public function get_roles($userid) {
		global $DB;

		return $DB->get_fieldset('roles', 'roleid', array(
			'userid' => $userid
		));
	}

	/**
	 * Add a role to a user.
	 */
	public function add_role($userid, $roleid) {
		global $DB, $USER;

		$roles = $this->get_roles($userid);

		if (!in_array($roleid, $role)) {
			$DB->insert_record('roles', array(
				'userid' => $userid,
				'roleid' => $roleid
			));
		}

		if ($USER->id == $userid) {
			$USER->roles = $this->get_roles($USER->id);
		}
	}

	/**
	 * Remove a role from a user.
	 */
	public function remove_role($userid, $roleid) {
		global $DB, $USER;

		$DB->delete_records('roles', array(
			'userid' => $userid,
			'roleid' => $roleid
		));

		if ($USER->id == $userid) {
			$USER->roles = $this->get_roles($USER->id);
		}
	}
}
<commit_msg>Fix add_role unique check<commit_after><?php
/**
 * Yet another blogging system.
 *
 * @license Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0.txt)
 * @author <NAME> <<EMAIL>>
 */

namespace Beam\Auth;

class User extends \Rapid\Auth\User
{
	const ROLE_VIEWER = 1;
	const ROLE_EDITOR = 2;
	const ROLE_ADMIN  = 4;

	/**
	 * Login hook.
	 */
	public function on_login() {
		global $USER;

		$USER->roles = $this->get_roles($USER->id);
	}

	/**
	 * Get roles for a user.
	 */
	public function get_roles($userid) {
		global $DB;

		return $DB->get_fieldset('roles', 'roleid', array(
			'userid' => $userid
		));
	}

	/**
	 * Add a role to a user.
	 */
	public function add_role($userid, $roleid) {
		global $DB, $USER;

		$roles = $this->get_roles($userid);

		if (!in_array($roleid, $roles)) {
			$DB->insert_record('roles', array(
				'userid' => $userid,
				'roleid' => $roleid
			));
		}

		if ($USER->id == $userid) {
			$USER->roles = $this->get_roles($USER->id);
		}
	}

	/**
	 * Remove a role from a user.
	 */
	public function remove_role($userid, $roleid) {
		global $DB, $USER;

		$DB->delete_records('roles', array(
			'userid' => $userid,
			'roleid' => $roleid
		));

		if ($USER->id == $userid) {
			$USER->roles = $this->get_roles($USER->id);
		}
	}
}

{'fraction_non_alphanumeric': 0.19976312672720095, 'fraction_numerical': 0.005527043031977891, 'mean_word_length': 3.047923322683706, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1738674', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1012, 'n_words': 213}