Lower User Roles not allowed to create higher Role

Can you just explain what it is your doing?
What are you expecting [:Username] to be?

1 Like

No I appreciate your help, I have got to grips with a lot of stuff and worked it out. This is possibly a little more advanced than I expected id get to

When A User is created using the above script, an entity of the same name needs to be created with the Entity Type being ‘Staff’.

OK, show your script so far so I can see what your thinking.

That does nothing if it’s not defined. Configure tasks use a special setting to define those. It’s called initialization.

On my way to a million likes LOL

2 Likes

holy poo!

Glad to have been of help! (in likes) lol

EDIT: Liked for good measure

Paste your script so we dont have any more variable confusion.

I think he attempted to use configuration task. That was not my intention. I just grabbed a jscript I was using in a configuration task to demonstrate the idea.

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 = 'Employee';

  var q="SELECT [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);
  
  //check for existing user with that name
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [Name]='" + usr + "'";
  var nameCount = sql.Query(q).First;

  //ceck for existing user with that pincode
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [PinCode]='" + pin + "'";
  var pinCount = sql.Query(q).First;

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

api.Entity('[:Username]').Create('Staff');
dlg.AskQuestion('Employee named [:Username] added.','OK');

I did, that’s been deleted now

Try +User+ inst3ad of [:UserName]

A) Your entity bit is outside the function. Move it to inside the first part of the if

B) Create() is for the entity name which if inside that function from earlier will be usr

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 = 'Employee';

  var q="SELECT [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);
  
  //check for existing user with that name
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [Name]='" + usr + "'";
  var nameCount = sql.Query(q).First;

  //ceck for existing user with that pincode
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [PinCode]='" + pin + "'";
  var pinCount = sql.Query(q).First;

  if (usr!='' && pin!='' && grp!='' && nameCount==0 && pinCount==0) {
    // run SQL to create the User
    q = "INSERT INTO [Users] ([PinCode], [Name], [UserRole_Id]) VALUES ('" + pin + "', '" + usr + "', '"+grp+"')";
   var r = sql.ExecSql(q);
   api.Entity(User).Create(usr);
   dlg.AskQuestion('Employee named '+usr+' added.','OK');

  }
dlg.AskQuestion('User or PinCode Already in use.','OK');

}

Ok so tried +usr+

I now have an entity called +usr+ lol

Updated above as missed a line

You only need to do ++ to combine sting and variable ie;

'startOfString'+variableInTheMiddle+'endOfString'

sting parts wraped in quotes and merged with +

Example although pointles;

var = 'startOfString'+' '+'endOfString'

would make a var string of

startOfString endOfString

1 Like

Message: ‘User’ is undefined

should that be changed to usr?

My bad, got them wrong way round, not used that function before.
https://sambapos.com/wiki/doku.php?id=jscript_api

api.Entity(name).Create(entityTypeName);

api.Entity(usr).Create(‘User’);

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 = 'Employee';

  var q="SELECT [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);
  
  //check for existing user with that name
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [Name]='" + usr + "'";
  var nameCount = sql.Query(q).First;

  //ceck for existing user with that pincode
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [PinCode]='" + pin + "'";
  var pinCount = sql.Query(q).First;

  if (usr!='' && pin!='' && grp!='' && nameCount==0 && pinCount==0) {
    // run SQL to create the User
    q = "INSERT INTO [Users] ([PinCode], [Name], [UserRole_Id]) VALUES ('" + pin + "', '" + usr + "', '"+grp+"')";
   var r = sql.ExecSql(q);
   api.Entity(usr).Create('User');
   dlg.AskQuestion('Employee named '+usr+' added.','OK');

  }

dlg.AskQuestion('User or PinCode Already in use.','OK');

}
1 Like

Works almost flawless! It creates the entity and the employee as expected but tells me the bit at the bottom of the script where it says ‘User or PinCode Already in use’ lol

(ill start your tab up soon)

Oh, shit yer, I changed the returns as you were using ask questions but not it will need to be an if else…

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 = 'Employee';

  var q="SELECT [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);
  
  //check for existing user with that name
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [Name]='" + usr + "'";
  var nameCount = sql.Query(q).First;

  //ceck for existing user with that pincode
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [PinCode]='" + pin + "'";
  var pinCount = sql.Query(q).First;

  if (usr!='' && pin!='' && grp!='' && nameCount==0 && pinCount==0) {
    
    // run SQL to create the User
    q = "INSERT INTO [Users] ([PinCode], [Name], [UserRole_Id]) VALUES ('" + pin + "', '" + usr + "', '"+grp+"')";
    var r = sql.ExecSql(q);
    
    // create user entity
    api.Entity(usr).Create('User');
    dlg.AskQuestion('Employee named '+usr+' added.','OK');
   
  } else {
   
    dlg.AskQuestion('User or PinCode Already in use.','OK');

  }
}