<?php
class User extends DBObject {
	public $Group;
	
	public static function HashPassword($password) {
		return hash("sha256",$password);
	}
	
	public function __construct($id=0) {
		$field="user_id";
		if (!is_numeric($id))
			$field="user_email";
		parent::__construct("users", $field, $id);
		
		$this->Group=new UserGroup($this->GroupId);
	}
	
	public function Save() {
		if (isset($this->_changedFields["UserPassword"]))
			$this->UserPassword=self::HashPassword($this->UserPassword);
		parent::Save();
	}
	
	public function ValidatePassword($password){
		return self::HashPassword($password)==$this->UserPassword;
	}
	
	public function HasAccess($permission) {	
		$perm=new Permission($permission);
		if ($perm->PermissionDefaultGrant==1)
			return true;
		
		return $this->Group->HasPermission($permission);
	}
}
