Forum Discussion

MikeTamoush's avatar
MikeTamoush
Qrew Commander
2 years ago

Custom Rule to ADD a record?

Ive seen some creative ideas in posts on how to regulate which users can delete records. However, haven't seen much in the way of selecting users who can add a record. 

I want to avoid using user roles for this, as I want to be able to have a handful of users be able to set this permission. ie - perhaps they have a checkbox on our employee table that says -User has add rights for Table X.

Then somehow use that checkbox field to allow that user to add a record in another table? I cant think of a way to accomplish this. Are there any tricks?



------------------------------
Mike Tamoush
------------------------------

6 Replies

  • If you have a table of users where the key field is the user, or even the users email address, then you can use that table as a user focus table. 

    Since the field is the user ID, you can relate that table to any other table through a reference field with the formula of the current user.  

    Then you can look up whether the current user is allowed to add records.  

    Then you would remove the ad button from most roles, except may be admin and make your own Add Record  button.   Of course, you'll have to figure out where to put this button. The button could be on existing records, or on some kind of parent record, or some kind of master  record where there's a dashboard on the homepage to take you to the special admin record, which would have the various restricted add buttons on them.  



    ------------------------------
    Mark Shnier (Your Quickbase Coach)
    mark.shnier@gmail.com
    ------------------------------
    • MikeTamoush's avatar
      MikeTamoush
      Qrew Commander

      Thats a good idea. Didn't think of the custom add button. I wish I could make a button like that on the new dashboards, but I suspect I would need to sneakily have a report showing with one record and the add button.

      My other idea is very similar to yours, but I actually let the user add, however use a dynamic form rule to hide all the sections, and unhide one that just says 'You do not have permissions to add this record, push cancel to abort.' 

      Or something like that, havent worked out all the details. Perhaps it also checks a box that causes the record to delete itself once saved.



      ------------------------------
      Mike Tamoush
      ------------------------------
  • Interestingly enough, I recently had a kinda similar use case. I typically try to stick with the auto-incrementing primary keys instead of altering the key field (e.g. to a User) for reasons I won't explain here. Anyway, it sounds like you could potentially leverage your existing Employees table in combination with Formula Queries and (conditional) Formula URL buttons to create a "Person Locator Service" or "Person Authorization Service." For example,

    // _DBID_EMPLOYEES
    // FID 3:    Record ID# (Integer)
    // FID 999:  Quickbase User (User)
    // FID 9999: Quickbase User Has xyz Permission (Checkbox)

    // Return the Current User's Record ID
    var Text QueryResults = ToText(GetFieldValues(GetRecords("{999.TV.'" & UserToId(User()) & "'}", [_DBID_EMPLOYEES]), 3));
    ToNumber($QueryResults)

    Instead of returning the Current User's Record ID (3 in the above example), you'd could return a boolean of whether the Employee has access to perform the given operation (e.g. Add the record). Maybe something like:

    var Bool HasPermission = ToText(GetFieldValues(GetRecords("{999.TV.'" & UserToId(User()) & "'}", [_DBID_EMPLOYEES]), 9999));

    Then, in a Formula URL (button) you can conditionally show or hide the button based on the results of the Formula Query.

    var Text UrlAddRecord =
        // Show form to create a new record
        URLRoot() & "db/" & [_DBID_THINGS] & "?a=API_GenAddRecordForm"

        // Set field value
        & "&_fid_xyz=" & URLEncode ([…])

        // Return to the context where this button was clicked
        & "&z=" & Rurl();

    If (
        // Conditionally display this button
        $HasPermission = true, $UrlAddRecord,

        // Default to hidden button
        null
    )

    I'm not saying any of this is a good idea, but it is interesting and sort of allows us to extend basic User info similar to a User Profile in other web apps. I'd consider building a playground Quickbase app to test the ideas before integrating it into your production app.

    The simpler version of all this would be to hardcode the allowable list of Users into a variable and then conditionally show the button if the current User is in the list.

    Use care with Formula Queries too, especially because they likely cause n + 1 query issues that I don't recall reading about yet!

    I'd also suggest really digging into why Quickbase's native Permission system doesn't accomplish what you are hoping to achieve.



    ------------------------------
    Brian Seymour
    ------------------------------
    • MikeTamoush's avatar
      MikeTamoush
      Qrew Commander

      Thanks for the reply. I am in fact deciding to do something similar to this. The sticking point really is this - I would love to have that conditionally shown button on home page or dashboard. That is really only possible if you sneakily do it by showing a report with 1 record on it. This works, but is not super clean looking. That is why I was originally hoping to actually limit who could add, so that if they clicked the button on the home page, it would say 'permission denied'.

      Though, it may be possible to make a custom button......I gotta play around with a few things. Ill report back if some ideas in my head work out.



      ------------------------------
      Mike Tamoush
      ------------------------------
      • TylerJablonski1's avatar
        TylerJablonski1
        Qrew Trainee

        One option is to have a button on the home page that points to a code page.  The code page can use the API to return the current user (API_GetUserInfo).  Then you can query the users table for that user and check whether or not they have permission.  If they do, you redirect them to the add form, otherwise you show them a "permission denied" message.  Of course, this doesn't solve the problem of conditionally showing the button on the home page.

        Another slightly more complicated option is that you have two home pages set up.  One for users who have access to add records, which will have the add record button, and one for everyone else that doesn't have the button.  Then in the user roles, you set the home page for everyone to a code page.  The code page uses the API to check the user's permission as in the first option, and then depending on whether or not they have access, it redirects them to one of the two "real" home pages.  That would solve the problem of conditionally showing the button.



        ------------------------------
        Tyler Jablonski
        ------------------------------