<commit_before><?php
namespace scv;

include_once 'core.php';



class UserException extends \Exception{}

class User {
	const SALT = 'ooj1nahMeeP4bohzJa2ied6eeir1IchijeyeiSh6zeG5TaiEang6peeiu0ohZie2';
	private $id = null;
	private $name = null;
	private $password = null;
	
	public function setId($id){
		$this->id = (int)$id;
	}
	
	public function setName($name){
		$this->name = (string)$name;
	}
	
	public function setPassword($pwd){
		$this->password = (string)$pwd;
	}
	
	public function alterPassword($newpassword,$oldpassword,$newuser=false){
		$core = Core::getInstance();
		$db = $core->getDB();
		
		if ((hash('ripemd160',$oldpassword.User::SALT) == $this->password) xor $newuser){
			$this->password = hash('<PASSWORD>',$newpassword.User::SALT);
			$this->store();
			$passwordqry = "SELECT USR_PASSWORD FROM USERS WHERE USR_ID = ?";
			$res = $db->query($core,$passwordqry,array($this->id));
			$set = $db->fetchObject($res);
			if ($this->password != $set->USR_PASSWORD){
				throw new UserException("Could not set Password: Some Error in Database");
			}
		}else{
			throw new UserException("Could not set Password: Old password is wrong");
		}
	}
	
	public function getName(){
		return $this->name;
	}
	
	public function getId(){
		return $this->id;
	}
	
	public function authenticate($password){
		$core = Core::getInstance();
		if (hash('ripemd160',$password.User::SALT) == $this->password){
			return true;
		}
		return false;
	} 
	
	public function delete($checkRight=true){
		$core = Core::getInstance();
		$userM = $core->getUserManager();
		$sessionUser = $userM->getSessionUser();
		$db = $core->getDB();
		
		$checkstring = "";
		if ($checkRight){
			$checkstring = "WHERE  1 = (SELECT AVAILABLE FROM CHECK_RIGHT(". $sessionUser->getId().",'scoville.users.delete')) ";
		}
		
		$stmnt="DELETE FROM USERS WHERE USR_ID = ? $checkstring ;";
		$res = $db->query($core,$stmnt,array($this->getId()));
		return;
	}
	
	public function store( $checkRight = true){
		$core = Core::getInstance();
		$db = $core->getDB();
		if ($this->id == null){
			$query = "INSERT INTO USERS (USR_ID, USR_NAME, USR_PASSWORD)
			          VALUES (?,?,?);";
		    $this->setId($db->getSeqNext('USR_GEN'));
		    $db->query($core,$query,array($this->id,$this->name,$this->password));
		}else{
			$query = "UPDATE USERS SET 
						USR_NAME = ?,
						USR_PASSWORD = ?
					  WHERE USR_ID = ?";
					  
		    $db->query($core,$query,array($this->name,$this->password,$this->id));
			if (ibase_errmsg() != false){
				throw new UserException("Storing User: Something went wrong in the Database");
			} 
		}
	}
	
	public function hasRole($role){
		$core = Core::getInstance();
		$rightM = $core->getRightsManager();
		return $rightM->hasRoleUser($role,$this);
	}
	
	public function getRights(){
		$core = Core::getInstance();
		$rightM = $core->getRightsManager();
		return $rightM->getRightsForUser($this);
	}
	
	public function getRoles(){
		$core = Core::getInstance();
		$rightM = $core->getRightsManager();
		return $rightM->getRolesForUser($this);
	}	
	
	public function grantRight($right, $checkRight=true){
		$core = Core::getInstance();
		$db = $core->getDB();
		$rightM = $core->getRightsManager();
		$userM = $core->getUserManager();
		$sessionUser = $userM->getSessionUser();
		$checkstring = "";
        if (!$rightM->checkRight('scoville.users.grant_revoke', $sessionUser)){
        	throw new UserException("Granting Right: This user is not allowed to grant rights!");
        }
		$rightId = $rightM->getIdForRight($right);
		if ($rightId == null){
			throw new UserException("Granting Right: There is no such right as $right");
		}
		if ($rightM->checkRight($right, $sessionUser) and $rightId != null){
			$db->query($core,"UPDATE OR INSERT INTO USERRIGHTS VALUES (?,?) MATCHING (URI_USR_ID,URI_RIG_ID) ;",array($this->id, $rightId));
			if (ibase_errmsg() != false){
				throw new UserException("Granting Right: Something went wrong in the Database");
			} 
		}
	}
	
	public function revokeRight($right, $checkRight=true){
		$core = Core::getInstance();
		$db = $core->getDB();
		$rightM = $core->getRightsManager();
		$userM = $core->getUserManager();
		$sessionUser = $userM->getSessionUser();
		$checkstring = "";
		if ($checkRight){
			$checkstring = " 1 = (SELECT AVAILABLE FROM CHECK_RIGHT(". $sessionUser->getId().",'scoville.users.grant_revoke')) ";
		}
		$rightId = $rightM->getIdForRight($right);
		if ($rightId == null){
			throw new UserException("Revoking Right: There is no such right as $right");
		}<commit_msg>Korrekte rechtekontroller in User::grantRight()<commit_after><?php
namespace scv;

include_once 'core.php';



class UserException extends \Exception{}

class User {
	const SALT = 'ooj1nahMeeP4bohzJa2<PASSWORD>';
	private $id = null;
	private $name = null;
	private $password = null;
	
	public function setId($id){
		$this->id = (int)$id;
	}
	
	public function setName($name){
		$this->name = (string)$name;
	}
	
	public function setPassword($pwd){
		$this->password = (string)$pwd;
	}
	
	public function alterPassword($newpassword,$oldpassword,$newuser=false){
		$core = Core::getInstance();
		$db = $core->getDB();
		
		if ((hash('ripemd160',$oldpassword.User::SALT) == $this->password) xor $newuser){
			$this->password = hash('<PASSWORD>',$newpassword.User::SALT);
			$this->store();
			$passwordqry = "SELECT USR_PASSWORD FROM USERS WHERE USR_ID = ?";
			$res = $db->query($core,$passwordqry,array($this->id));
			$set = $db->fetchObject($res);
			if ($this->password != $set->USR_PASSWORD){
				throw new UserException("Could not set Password: Some Error in Database");
			}
		}else{
			throw new UserException("Could not set Password: Old password is wrong");
		}
	}
	
	public function getName(){
		return $this->name;
	}
	
	public function getId(){
		return $this->id;
	}
	
	public function authenticate($password){
		$core = Core::getInstance();
		if (hash('ripemd160',$password.User::SALT) == $this->password){
			return true;
		}
		return false;
	} 
	
	public function delete($checkRight=true){
		$core = Core::getInstance();
		$userM = $core->getUserManager();
		$sessionUser = $userM->getSessionUser();
		$db = $core->getDB();
		
		$checkstring = "";
		if ($checkRight){
			$checkstring = "WHERE  1 = (SELECT AVAILABLE FROM CHECK_RIGHT(". $sessionUser->getId().",'scoville.users.delete')) ";
		}
		
		$stmnt="DELETE FROM USERS WHERE USR_ID = ? $checkstring ;";
		$res = $db->query($core,$stmnt,array($this->getId()));
		return;
	}
	
	public function store( $checkRight = true){
		$core = Core::getInstance();
		$db = $core->getDB();
		if ($this->id == null){
			$query = "INSERT INTO USERS (USR_ID, USR_NAME, USR_PASSWORD)
			          VALUES (?,?,?);";
		    $this->setId($db->getSeqNext('USR_GEN'));
		    $db->query($core,$query,array($this->id,$this->name,$this->password));
		}else{
			$query = "UPDATE USERS SET 
						USR_NAME = ?,
						USR_PASSWORD = ?
					  WHERE USR_ID = ?";
					  
		    $db->query($core,$query,array($this->name,$this->password,$this->id));
			if (ibase_errmsg() != false){
				throw new UserException("Storing User: Something went wrong in the Database");
			} 
		}
	}
	
	public function hasRole($role){
		$core = Core::getInstance();
		$rightM = $core->getRightsManager();
		return $rightM->hasRoleUser($role,$this);
	}
	
	public function getRights(){
		$core = Core::getInstance();
		$rightM = $core->getRightsManager();
		return $rightM->getRightsForUser($this);
	}
	
	public function getRoles(){
		$core = Core::getInstance();
		$rightM = $core->getRightsManager();
		return $rightM->getRolesForUser($this);
	}	
	
	public function grantRight($right, $checkRight=true){
		$core = Core::getInstance();
		$db = $core->getDB();
		$rightM = $core->getRightsManager();
		$userM = $core->getUserManager();
		$sessionUser = $userM->getSessionUser();
		if ($checkRight){
	        if (!$rightM->checkRight('scoville.users.grant_revoke', $sessionUser)){
	        	throw new UserException("Granting Right: This user is not allowed to grant rights!");
	        }
		}
		$rightId = $rightM->getIdForRight($right);
		if ($rightId == null){
			throw new UserException("Granting Right: There is no such right as $right");
		}
		if ($rightId != null){
			if($checkRight and !$rightM->checkRight($right,$sessionUser)){
				return;
			}
			$db->query($core,"UPDATE OR INSERT INTO USERRIGHTS VALUES (?,?) MATCHING (URI_USR_ID,URI_RIG_ID) ;",array($this->id, $rightId));
			if (ibase_errmsg() != false){
				throw new UserException("Granting Right: Something went wrong in the Database");
			} 
		}
	}
	
	public function revokeRight($right, $checkRight=true){
		$core = Core::getInstance();
		$db = $core->getDB();
		$rightM = $core->getRightsManager();
		$userM = $core->getUserManager();
		$sessionUser = $userM->getSessionUser();
		$checkstring = "";
		if ($checkRight){
			$checkstring = " 1 = (SELECT AVAILABLE FROM CHECK_RIGHT(". $sessionUser->getId().",'scoville.users.grant_revoke')) ";
		}
		$rightId = $rightM->getIdForRight($right);
		if ($rightId == null){
			throw new UserException("Revoking Right: There is no such right as $right");
		}
{'fraction_non_alphanumeric': 0.17979376919701623, 'fraction_numerical': 0.003181219833260202, 'mean_word_length': 4.279096699478865, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 114, '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': '63265', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3068, 'n_words': 661}