<commit_before><?php namespace Volicon\Acl\Models;

use Volicon\Acl\AclRole;
use Illuminate\Support\Facades\Config;

class UserRole extends \Eloquent {
	protected $table = 'user_role';
	protected $fillable = [ 
			'user_id',
			'role_id' 
	];
	protected $primaryKey = 'role_id';
	public $timestamps = FALSE;
	
	public function role() {
		return $this->hasOne ( 'Volicon\Acl\Models\Role', 'role_id' );
	}

	public static function updateRoleUsers(AclRole $role) {
		
		$current_users = UserRole::where('role_id', '=', $role->role_id)->lists('user_id');
		
		$users_to_delete = array_diff($current_users, $role->users->toArray());
		if($users_to_delete) {
			static::where ( 'role_id', '=', $role->role_id )->whereIn('user_id', $users_to_delete)->delete();
			
			$default_role_id = static::getDefaultRoleId();
			if($default_role_id) {
				$users_with_roles = static::whereIn('user_id', $users_to_delete)->get()->lists('user_id');
				$user_without_roles = array_diff($users_to_delete, $users_with_roles);
				foreach($user_without_roles as $user_id) {
					UserRole::create ( [ 
						'role_id' => $default_role_id,
						'user_id' => $user_id 
				] );
				}
			}
		}
		
		$users_to_add = array_diff($role->users->toArray(), $current_users);
		
		if($users_to_add) {<commit_msg>Not allowed to remove defults users from their roles.<commit_after><?php namespace Volicon\Acl\Models;

use Volicon\Acl\AclRole;
use Illuminate\Support\Facades\Config;

class UserRole extends \Eloquent {
	protected $table = 'user_role';
	protected $fillable = [ 
			'user_id',
			'role_id' 
	];
	protected $primaryKey = 'role_id';
	public $timestamps = FALSE;
	
	public function role() {
		return $this->hasOne ( 'Volicon\Acl\Models\Role', 'role_id' );
	}

	public static function updateRoleUsers(AclRole $role) {
		
		$current_users = UserRole::where('role_id', '=', $role->role_id)->get();
		$defaults_users = $current_users->where('default')->lists('user_id');
		
		$users_to_delete = array_diff($current_users->lists('user_id'), $role->users->toArray(), $defaults_users));
		if($users_to_delete) {
			static::where ( 'role_id', '=', $role->role_id )->whereIn('user_id', $users_to_delete)->delete();
			
			$default_role_id = static::getDefaultRoleId();
			if($default_role_id) {
				$users_with_roles = static::whereIn('user_id', $users_to_delete)->get()->lists('user_id');
				$user_without_roles = array_diff($users_to_delete, $users_with_roles);
				foreach($user_without_roles as $user_id) {
					UserRole::create ( [ 
						'role_id' => $default_role_id,
						'user_id' => $user_id 
				] );
				}
			}
		}
		
		$users_to_add = array_diff($role->users->toArray(), $current_users);
		
		if($users_to_add) {
{'fraction_non_alphanumeric': 0.16784255477163015, 'fraction_numerical': 0.0, 'mean_word_length': 4.695560253699789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '6595226', 'n_tokens_mistral': 1101, 'n_tokens_neox': 978, 'n_words': 149}