-
Notifications
You must be signed in to change notification settings - Fork 0
Rules
When a Telegram user sends messages to the bot it might be useful to check some properties, for example if he is allowed access to the bot or his legal permit has expired. These are the rules. In the entities/authorization_rules/ folder there are the files that handle the rules.
For each of these properties you can create a new class that extends the Rule base class (as, in the example, CheckIfUserIsActiveRule and CheckIfUserIsSubscribedRule) and define the rule() method, as in the example below:
// for example, the bot can be used only from users directly activated from an Admin
class CheckIfUserIsActiveRule extends Rule {
public function rule() {
if ($this->getUser()->isActive()) {
return true;
}
return false;
}
}Then you have to add the instances of the classes you've created in the private function rulesToAdd(), inside the UserAuthorization class:
private function rulesToAdd() {
$this->addRule( new CheckIfUserIsActiveRule($this->getUser()) );
$this->addRule( new CheckIfUserIsSubscribedRule($this->getUser()) );
}The UserAuthorization class takes all the rules classes and with the method verifyAuthorization() will execute the rule() method of every instance you have added in the rulesToAdd() method. So, you can verify all the necessary rules and also get specific error messages for each one, in case a rule doesn't pass the check.
The basic rules inserted as example in the starting project are invoked for every webhook request, as they are in the initRequestProcessing() method (more info here). You can use the rules logic as you wish, to make request processing more robust.