Lower User Roles not allowed to create higher Role

fairly simple in that your just inserting your variables into the sql query in its string format so is fairly visual…

1 Like

So I’m reading the script, and I have absolutely no idea where I’m to start? I can understand the script in areas and see what its meant to do, but I cant even think of what to do with it?

Changed from V5 Request to V5 Question

You declare your query as a string ie ‘qry’ in sample.
Where you want to use a variable in the query you end the string put a + then your variable then another + and reopen the string.
Then you execure the query with sql.ExecSql(qry) where qry is the variable for the string.

so variable being [?UserName] ?

Sorry, I think I’m getting there…

Or am I creating a new Action via ‘Update Program Setting’…

My first thing to try would be using [?prompt] in the call to see if that works and feed directly into the function;

Something like;
{CALL:UserManager.createuser('[?Username]','[?Pin]')} <- cant remember if the ‘’ would be needed or not.

If that works you can just feed it straight in to script nice and easy :slight_smile: but not tried…

Good news is it did ‘Something’. in terms where it prompted for a Username and Pin

It didn’t add anything in the users though.

When you say ‘’ where did you mean to add? do you mean in from the { and } so [?Pin]‘)’}

This is the JScript helper version of [?prompt]

###dlg.EditValue()

Examples:

dlg.EditValue("Enter Username;;;","");
dlg.EditValue("Enter PIN;.{4};;ON","");

But using it in JScript will probably produce one prompt, then another…

Whereas if you use [?prompt] notation in a Rule to set Program Setting values in 2 separate Actions, one right after the other, only a single prompt “page” will be shown, with 2 boxes for input.

@JTRTech’s example might work, but it might also produce 2 prompt “pages”, or not, if it even works.

It comes down to the type of flow you want. Personally, I would use prompt in 2 actions to set Local Program Settings (ie. Username and PIN), then another action (Execute Script) to execute the script. Then in the JScript, you do:

Handler: userManager

function createUser(u,p) {
 // can get User and Pin fed via function or 
 // use Data.Get to retrieve Program Settings
  var usr = (typeof u === 'undefined' || u=='' ? Data.Get("Username") : u);
  var pin = (typeof p === 'undefined' || p=='' ? Data.Get("PIN") : p);
  var grp = 'Cashier';

  var q="SELECT [UserRole_Id] FROM [UserRoles] WHERE [Name]='" + grp + "'";
  var grp = sql.Query(q).First();

  // more validation
  usr = (typeof usr === 'undefined' ? '' : usr;
  pin = (typeof pin === 'undefined' ? '' : pin;
  grp = (typeof grp === 'undefined' ? '' : grp;

  if (usr!='' && pin!='' && grp!='') {
   // run SQL to create the User
    q = "INSERT INTO [Users] ([PinCode], [Name], [UserRole_Id]) VALUES ('" + pin + "', '" + usr + "', '"+grp+"')";
    var r = sql.ExecSql(q);
    return r;
  }
  return false;
}

NOTE: untested, some syntax might be incorrect, but you get the idea.


##Actions

##USR Store Setting [Update Program Setting] (Action)##

Action Name: USR Store Setting
Action Type: Update Program Setting
###Parameters:###
Setting Name: [:settingName]
Setting Value: [:settingValue]
Update Type: Update
Is Local: True

##USR ExecScript [Execute Script] (Action)##

Action Name: USR ExecScript
Action Type: Execute Script
###Parameters:###
Function: [:handler.func]
Command:
Run In Background: False

##Rule

Two ways to do this:

Just execute the script with no parameters, and the JScript will use Data.Get(), or feed parameters into the function …

##USR Create User [Automation Command Executed] (Rule)##

Rule Name: USR Create User
Event Name: Automation Command Executed
Rule Tags:
Custom Constraint List (1):
Execute Rule if: Matches
Automation Command NameEqualsUSR Create User

##Actions (3):##

USR Store Setting

Constraint: (none)

settingName: Username
settingValue: [?Enter Username]
USR Store Setting

Constraint: (none)

settingName: PIN
settingValue: [?Enter PIN]
USR ExecScript

Constraint: (none)

handler.func: userManager.createUser('{SETTING:Username}','{SETTING:PIN}')

##Mappings##

Mappings
Terminal User Role Department Ticket Type
****

3 Likes

Awesome, will definatly use that in the future, cheers Q, missed that one…

From my experiance in that quick hotel setup multi ?prompts in a single rule seem to get merged into a multi field prompt… that is if it were to work.
Either way the helper is much cleaner :wink:

Whats this bit? not used type of before… looks like a ternary to make undefined SQL result ‘’?

It is a “special” check to see if the value is undefined. Depending on ECMAscript level supported, which in SambaPOS is only V3), this does not work …

if (myVar == 'undefined') then turn it into an empty string ''

So we must do this “special” check instead:

if (typeof myVar === 'undefined') then turn it into an empty string ''

Some versions even treat 'undefined' and empty strings as a “falsies”, so this might work as well to test for both conditions:

if (myVar) then do something, otherwise throw an error that the var is empty/undefined/false/zero
2 Likes

Hmmm, would have been surprised if that didnt come up in my PMS scripts… I would have just presumed undefined and done;
if (!myVar)
But maybe the issue didnt come up for those scripts…

Right, see my last comment regarding that, about “falsies”. It might be that even V3 supports that, I don’t know, so your way of checking might work. But sometimes just best to be thorough enough to check it anyway.

EDIT: interesting, also note

// An undeclared variable cannot be used in a comparison without
// the typeof operator, so the next line generates an error
// because the variable "notDeclared" was never declared
var result = (notDeclared === undefined); // ERROR
// so do this instead, if using undeclared variables to avoid errors
var result = (typeof notDeclared === 'undefined'); // result will contain true

Reference: typeof - JavaScript | MDN

1 Like

Hmmmm, but by undeclared, if you had declared it as ‘’ in the beginning before query would that also potentally solve? Sure I did have some issues where delaring the variable before (know its good practice to do anyway) solved if I remember right, maybe related.

Although wouldnt work in your example as you have doubled it up with the option of inputting u and p in the command…

Yes, which is what we should strive to do, as you say, as good practice. You don’t actually need to assign it to declare it. So this would work:

var wasNotDeclaredButNowItIs; // no assignment, but it is declared
1 Like

Tidy tip :slight_smile:

Right, so this is where typeof really comes in. Are the variables u and p actually “declared” because they are function parameters? I don’t think so, so we need to use typeof to check them.


In other languages, and later versions of JS, we can assign default values, and even declare the parameters which is nice, but we are not that lucky with V3 …

function myFunc(var u='', var p='1234') {
  // stuff
}
1 Like

Is this a similar senario to !$var and isset($var) for php? as still havent quite got my head round the difference of those.

Sure. :wink: PHP has some better constructs in it, though I rarely use isset() … but I am no expert.