1

I have a custom views query plugin that pull data from a remote API. I use this plugin to save all information so user don't have to enter all details anymore.

User inputs value to search filter and all mapped data is displayed on the page.

I want to limit the number of attempt allowed to a user of 3 searches every 24 hours. So in a day, user can have 3 attempts to make a successful query of data from the remote api. Then, there might me a warning message that appears on the page after those failure attempts. Each day is equivalent to a calendar date.

$vats is where user input value is set in the view search filter. This value is an integer.

$vats = isset($filters['vat']) ? $filters['vat'] : NULL;
$Vid = $vats[0];

$result = CVRClient::get($Vid, $country, 'Unit Testing');

Here's the path of my view page with arguments: query/query?vat=12345678, 1st 'query' is the sitename and 2nd is the menu path name.

Can this be achieve by Rules? If yes, I would appreciate if someone can guide me ways to do it.

7
  • @Pierre.Vriens, thanks. I added more details on my issue. Commented Feb 14, 2017 at 11:06
  • Did you mean the endpoint of the remote api? Or to where I write the custom view plugin? Commented Feb 14, 2017 at 12:17
  • Sorry, but I did not mean either one. Actually, what I wonder about is about some "path" that I could use. Because if that's the case, then it's a piece of cake to do what you're asking for. But possibly such path makes no sense to you (and hence what I'm thinking of would probably not work either), in the context of "this" question. If that's the case: sorry. Commented Feb 14, 2017 at 12:21
  • I explain a bit so I can also understand what path your talking about, or maybe you can give me an example. All users is registered via Keycloak and page will redirect to Drupal site after its successful registration. This redirected page is the custom view plugin created. For users to be able to add its company details. And so users will enter their VAT # to get data from the CVR Register. The rest the following actions is explained above. Commented Feb 14, 2017 at 12:29
  • Alright, i see... here: query/query?vat=12345678. First 'query' is the sitename, then 2nd is the menu path name. Commented Feb 14, 2017 at 12:47

1 Answer 1

1

Preface

You added an interesting comment below your question, part of it is like so:

All users is registered via Keycloak and page will redirect to Drupal site after its successful registration. This redirected page is the custom view plugin created. For users to be able to add its company details. And so users will enter their VAT # to get data from the CVR Register. ...

Followed by this extra comment:

query/query?vat=12345678. First 'query' is the sitename, then 2nd is the menu path name

So you have a relative path like in this example /query?vat=12345678. And therefor the answer to your question (= Can this be achieve by Rules?) is Yes, Si, Oui, Ja ... Read on for more details to guide you ...

Solution

You may want to try the "Rules URL argument" module. Here is a quote about it (from its project page):

... provides two rules conditions based on URL arguments:

  • check if a URL argument is present.
  • compare the value of an URL argument.

It also provides an action that makes the value of an URL argument available as new variable to Rules.

The newly provided Rules actions and conditions can be found under the "URL Argument" conditions and actions groups in the rule configuration interface.

For an example of how to use it, refer to Comment # 3 in issue # 1686360 which is about "Pass an amount to the url, ie; site/content/node-title?amount=10 or /node-title?=amount:10". Here is the relevant part of it:

  • add a "Check if URL argument exist" condition to your rule and set the "ARGUMENT" value to "amount".
  • add "Provide URL argument value" action to the rule and set "URL ARGUMENT NAME" value to "amount" and in the "Provided variables" section "ARGUMENT FROM URL - Variable name" to e.g. "amount_from_url".

You will now be able to use the amount value as a variable "amount-from-url" for other actions in this rule.

So with that you can use Rules to (let's say) "monitor" the usage of that path by an authenticated user. Some more details about the custom rule you could create for this:

  • Rules Event: drupal is initializing (sorry, that's probably the only one that you would be able to use).

  • Rules Conditions (pick whatever fits, and/or mix however you want):

    • user has a specific role (or the negation of it).
    • check the current path (verify if it starts with query).
    • check if URL argument vat= is present.

The only thing left to do is to think of something appropriate for some Rules Action(s) to be performed, knowing that you do have these crucial data available:

  • the current user who tries to use this path again.
  • the actual value (=12345678) that was used.

Obviously, at a minimum you'd want to add a Rules Action to perform an appropriate "redirect" if the 3 attempts have been used on the same day already.

You'd have to do some more homework/refinements, but here is a blueprint of a basic solution that could work:

  • Solution 1 (easiest approach to implement):

    • Use the Flag module (eg with 3 none-global flags labeled like attempt 1, attempt 2 and attempt 3).
    • Add a Rules Action to flag the user with the relevant flag (corresponding to the number of succeeded attempts before).
    • Add an extra Rules Condition to verify that not all flags have been used (with the same flagging date!).
  • Solution 2 (= my recommended approach, though a bit tougher to implement): Use the Message module to create a custom activity log, to log each event where the actual URL has been used (with success). And improve the Rule above to add a rules condition to count the number of such messages (= custom activity log records) created already in the last 24 hours.

PS: I'm assuming this is about D7 ...

4
  • Alright! thank you so much.. this solution is excellent as it is so detailed :D Will work on it now. Commented Feb 14, 2017 at 13:45
  • Just wondering... Im using D8, and I see that the "Rules URL argument" module is still on D7. Commented Feb 14, 2017 at 13:52
  • That's a problem, and also why I added my PS. For the solution in my answer to work, I think you have 2 options: (1) wait for a D8 release (2) downgrade your site to D7. However, there are plenty of other reasons why the answer to "Can this be achieved with Rules" is no if you're using D8. Commented Feb 14, 2017 at 14:03
  • Alright, I understand...but I have another approach, is there any way to hide the views UI for a day after making 3 input mistakes in the VAT value? So it would be difficult for users to guess a other's VAT though this is a public remote registry. Commented Feb 14, 2017 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.