home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / progs / ite / ite10d1.exe / DATA.Z / SECURITY.JS < prev    next >
Encoding:
Text File  |  1996-09-07  |  65.6 KB  |  1,614 lines

  1. #define SM_PASSWORD "masterkey"
  2. #include <security.h>
  3.  
  4. /****************************************************************************\
  5. *                                                                            *
  6. * Classname   SecurityManager                                                *
  7. *                                                                            *
  8. * Purpose     Security class for non-administrative purposes. Keeps track    *
  9. *             of one user. Allows login and querying of access privileges.   *
  10. *                                                                            *
  11. * Dependencies:                                                              *
  12. *                                                                            *
  13. *    The SecurityManager class needs access to two tables, SMENTITY and      *
  14. *    SMASSIGN. These files are found by using a BDE alias named              *
  15. *    SecurityManager. You must use the BDE Configuration Utility to create   *
  16. *    a Standard alias by this name that points to the directory containing   *
  17. *    these two tables.                                                       *
  18. *                                                                            *
  19. *    Additionally, SECURITY.H is located in the IntraBuilder\Include         *
  20. *    directory.                                                              *
  21. *                                                                            *
  22. * Properties:                                                                *
  23. *                                                                            *
  24. *    classname - "SecurityManager"                                           *
  25. *                                                                            *
  26. * Methods:                                                                   *
  27. *                                                                            *
  28. *   changeDescription( <new description> )                                   *
  29. *   changePassword( <old>, <new> )                                           *
  30. *   getCreated()                          // return date created             *
  31. *   getDescription()                      // return description              *
  32. *   getGroups()                           // return AssocArray of groups     *
  33. *   getLogin()                  // return date of last (current) login       *
  34. *   getPolicyValue( <policy name> )       // return policy value             *
  35. *   getResources()                        // return AssocArray of resources  *
  36. *   getUserName()                         // return user name                *
  37. *   hasAccessTo( <resource name> )        // return true/false               *
  38. *   isMemberOf( <group name> )             // return true/false              *
  39. *   login( <user name>, <password> )                                         *
  40. *                                                                            *
  41. * Exceptions thrown:                                                         *
  42. *                                                                            *
  43. *   Constructor and methods may throw exceptions of class SmException.       *
  44. *   Calls to this class should be made within a try block. Security          *
  45. *   specific exceptions can be caught as SmException objects. See example.   *
  46. *                                                                            *
  47. * Example:                                                                   *
  48. *                                                                            *
  49. *   #include "security.h"  // defines SmException code values                *
  50. *   try {                                                                    *
  51. *      var x = new SecurityManager();                                        *
  52. *      x.login("sysdba","masterkey");                                        *
  53. *   }                                                                        *
  54. *   catch (SmException e) {                                                  *
  55. *      alert( "A security error has occured (" + e.code + ")" );             *
  56. *   }                                                                        *
  57. *                                                                            *
  58. * Updated 8/27/96 by IntraBuilder Samples Group                              *
  59. * $Revision:   1.3  $                                                        *
  60. *                                                                            *
  61. * Copyright (c) 1996, Borland International, Inc. All rights reserved.       *
  62. *                                                                            *
  63. \****************************************************************************/
  64. class SecurityManager {
  65.    this.classname    = "SecurityManager";
  66.    // 
  67.    // The _policy property is the same for all users
  68.    //
  69.    this._policy      = new AssocArray();
  70.  
  71.    //
  72.    // User specific properties. Set in login() method.
  73.    //
  74.    this._created     = null;
  75.    this._description = null;
  76.    this._group       = new Array();
  77.    this._login       = null;
  78.    this._resource    = new Array();
  79.    this._username    = null;
  80.  
  81.    // 
  82.    // Check to see if the SecurityManager alias exists
  83.    // 
  84.    this._session = new Session();
  85.    this._database = new Database();
  86.    this._database.session = this._session;
  87.    this._database.databaseName = SM_DATABASE_ALIAS;
  88.    this._database.session.addPassword( SM_PASSWORD );
  89.    try {
  90.       this._database.active = true;
  91.    }
  92.    catch (Exception e) {
  93.       throw new SmException(SM_ERROR_BDE_ALIAS_MISSING);
  94.    }
  95.  
  96.    // store the policies to the _policy array
  97.    var tPolicy = new Query();
  98.    tPolicy.database = this._database;
  99.    tPolicy.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  100.                  + SM_ENTITY_POLICY ;
  101.    tPolicy.active = true;
  102.    var rs = tPolicy.rowset;
  103.    while (!rs.endOfSet) {
  104.       this._policy[rs.fields["Entity Name"].value] = 
  105.          ( rs.fields["Policy Boolean"].value ? 
  106.            (rs.fields["Policy Value"].value != 0) :
  107.            (rs.fields["Policy Value"].value) );
  108.       rs.next();
  109.    }
  110.  
  111.  
  112.    function changePassword(oldPass, newPass) {
  113.       // make sure there is a current user
  114.       if (this._username == null) 
  115.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  116.  
  117.       // check that the parameters are valid
  118.       if (oldPass == null)
  119.          throw new SmException(SM_ERROR_INVALID_PASSWORD);
  120.       if (newPass == null)
  121.          throw new SmException(SM_ERROR_INVALID_PASSWORD);
  122.  
  123.       // new password must meet min/max policy requirements
  124.       if (newPass.length < this.getPolicyValue("PASSMIN"))
  125.          throw new SmException(SM_ERROR_PASSWORD_TOO_SHORT);
  126.       if (newPass.length > this.getPolicyValue("PASSMAX"))
  127.          throw new SmException(SM_ERROR_PASSWORD_TOO_LONG);
  128.  
  129.       // find this user in the smentity table
  130.       var tUser = new Query();
  131.       tUser.database = this._database;
  132.       tUser.sql = 'select * from smentity sm where sm."Entity Name" = "' +
  133.                   this._username + '" and sm."Entity Type" = ' + SM_ENTITY_USER ;
  134.       tUser.active = true;
  135.       var fld = tUser.rowset.fields;
  136.  
  137.       //
  138.       // Look for various error conditions
  139.       //
  140.  
  141.       // username not found
  142.       if (tUser.rowset.endOfSet)
  143.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  144.  
  145.       // user account is currently disabled
  146.       if (fld["User Disabled"].value)
  147.          throw new SmException(SM_ERROR_LOGIN_DISABLED);
  148.  
  149.       // invalid password
  150.       if ((this.getPolicyValue('CASE') 
  151.            && fld["User Password"].value != oldPass) ||
  152.           (!this.getPolicyValue('CASE') 
  153.            && fld["User Password"].value.toUpperCase() != oldPass.toUpperCase()))
  154.          throw new SmException(SM_ERROR_INVALID_PASSWORD);
  155.  
  156.       // OK, write the new password
  157.       fld["User Password"].value = newPass;
  158.       tUser.rowset.save();
  159.  
  160.       return (true);
  161.    }
  162.  
  163.    function changeDescription( newDescription ) {
  164.       // make sure there is a current user
  165.       if (this._username == null) 
  166.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  167.  
  168.       // find this user in the smentity table
  169.       var tUser = new Query();
  170.       tUser.database = this._database;
  171.       tUser.sql = 'select * from smentity sm where sm."Entity Name" = "' +
  172.                   this._username + '" and sm."Entity Type" = ' + SM_ENTITY_USER ;
  173.       tUser.active = true;
  174.       var fld = tUser.rowset.fields;
  175.  
  176.       //
  177.       // Look for various error conditions
  178.       //
  179.  
  180.       // username not found
  181.       if (tUser.rowset.endOfSet)
  182.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  183.  
  184.       // user account is currently disabled
  185.       if (fld["User Disabled"].value)
  186.          throw new SmException(SM_ERROR_LOGIN_DISABLED);
  187.  
  188.       // OK, write the new description
  189.       fld["Description"].value = ("" + newDescription);
  190.       tUser.rowset.save();
  191.       this._description = ("" + newDescription);
  192.  
  193.       return (true);
  194.    }
  195.  
  196.    function getCreated() {
  197.       // make sure there is a current user
  198.       if (this._username == null) 
  199.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  200.  
  201.       return this._created;
  202.    }
  203.  
  204.    function getDescription() {
  205.       // make sure there is a current user
  206.       if (this._username == null) 
  207.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  208.  
  209.       return this._description;
  210.    }
  211.  
  212.    function getGroups() {
  213.       // make sure there is a current user
  214.       if (this._username == null) 
  215.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  216.  
  217.       return this._group;
  218.    }
  219.  
  220.    function getLogin() {
  221.       // make sure there is a current user
  222.       if (this._username == null) 
  223.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  224.  
  225.       return this._login;
  226.    }
  227.  
  228.    function getPolicyValue( policyName ) {
  229.       return (this._policy.isKey(policyName) ? this._policy[policyName] : null);
  230.    }
  231.  
  232.    function getResources() {
  233.       // make sure there is a current user
  234.       if (this._username == null) 
  235.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  236.  
  237.       return this._resource;
  238.    }
  239.  
  240.    function getUserName() {
  241.       // make sure there is a current user
  242.       if (this._username == null) 
  243.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  244.  
  245.       return this._username;
  246.    }
  247.  
  248.    function hasAccessTo( resourceName ) {
  249.       // make sure there is a current user
  250.       if (this._username == null) 
  251.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  252.  
  253.       return (this._resource.isKey(resourceName.toUpperCase()));
  254.    }
  255.  
  256.    function isMemberOf( groupName ) {
  257.       // make sure there is a current user
  258.       if (this._username == null) 
  259.          throw new SmException(SM_ERROR_NOT_LOGGED_IN);
  260.  
  261.       return (this._group.isKey(groupName.toUpperCase()));
  262.    }
  263.  
  264.    function login(name, password) {
  265.       // reset the user information
  266.       this._admin       = false;
  267.       this._created     = null;
  268.       this._description = null;
  269.       this._group       = new AssocArray();
  270.       this._login       = null;
  271.       this._resource    = new AssocArray();
  272.       this._username    = null;
  273.  
  274.       // check that the parameters are valid
  275.       if (name == null)
  276.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  277.       if (password == null)
  278.          throw new SmException(SM_ERROR_INVALID_PASSWORD);
  279.  
  280.       // convert username to uppercase
  281.       var username = name.toUpperCase();
  282.  
  283.       // find this user in the smentity table
  284.       var tEntity = new Query();
  285.       tEntity.database = this._database;
  286.       tEntity.sql = 'select * from smentity sm where sm."Entity Name" = "' +
  287.                   username + '" and sm."Entity Type" = ' + SM_ENTITY_USER ;
  288.       tEntity.active = true;
  289.       var fld = tEntity.rowset.fields;
  290.  
  291.       //
  292.       // Look for various error conditions
  293.       //
  294.  
  295.       // username not found
  296.       if (tEntity.rowset.endOfSet)
  297.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  298.  
  299.       // user account is currently disabled
  300.       if (fld["User Disabled"].value)
  301.          throw new SmException(SM_ERROR_LOGIN_DISABLED);
  302.  
  303.       // user account is locked out
  304.       if (fld["User Lockout"].value) {
  305.          // see if autoreset is allowed
  306.          if (this.getPolicyValue('AUTORESET')) {
  307.             // if autoreset is allowed, then see if necessary time has elapsed
  308.             var loDate = new Date("" + fld["User Lockout Time"].value);
  309.             var resetDate = new Date(loDate.getTime()+(60000*this.getPolicyValue("LOMINUTES")));
  310.             var nowDate = new Date();
  311.             if (nowDate.getTime() > resetDate.getTime()) {
  312.                // reset time has expired, clear all the lockout flags
  313.                fld["User Lockout"].value = false;
  314.                fld["User Lockout Count"].value = 0;
  315.                fld["User Lockout Time"].value = null;
  316.                tEntity.rowset.save();
  317.             }
  318.          }
  319.       }
  320.  
  321.       // check lockout again (it may have been reset above)
  322.       if (fld["User Lockout"].value)
  323.          throw new SmException(SM_ERROR_LOGIN_LOCKOUT);
  324.  
  325.       // All that's left now is the password. Either say, we need to update
  326.       // the lockout information if the lockout policy is in use.
  327.       var locount = this.getPolicyValue("LOCOUNT");
  328.  
  329.       // invalid password
  330.       if ((this.getPolicyValue('CASE') 
  331.            && fld["User Password"].value != password) ||
  332.           (!this.getPolicyValue('CASE') 
  333.            && fld["User Password"].value.toUpperCase() != password.toUpperCase())) {
  334.          // If the lockout policy is in use, set the lockout flags for this user.
  335.          if (locount > 0) {
  336.             // see if old data is still relevant
  337.             var loDate = new Date("" + fld["User Lockout Time"].value);
  338.             var resetDate = new Date(loDate.getTime()+(60000*this.getPolicyValue("LOMINUTES")));
  339.             var nowDate = new Date();
  340.             if (nowDate.getTime() > resetDate.getTime())
  341.                fld["User Lockout Count"].value = 1;
  342.             else
  343.                fld["User Lockout Count"].value += 1;
  344.             fld["User Lockout Time"].value = new Date();
  345.             if (fld["User Lockout Count"].value >= locount)
  346.                fld["User Lockout"].value = true;
  347.             tEntity.rowset.save();
  348.          }
  349.          throw new SmException(SM_ERROR_INVALID_PASSWORD);
  350.       }
  351.  
  352.       // Successful login. Set login info. 
  353.       // Reset lockout info if lockout policy is in use.
  354.       if (locount > 0 && fld["User Lockout Count"].value > 0) {
  355.          // clear old error information
  356.          fld["User Lockout"].value = false;
  357.          fld["User Lockout Count"].value = 0;
  358.          fld["User Lockout Time"].value = null;
  359.       }
  360.       fld["User Login"].value = new Date();
  361.       tEntity.rowset.save();
  362.  
  363.       //
  364.       // If no error of any kind have occured, then set the user values.
  365.       //
  366.       this._created     = fld["Created"].value;
  367.       this._description = fld["Description"].value;
  368.       this._login       = fld["User Login"].value;
  369.       this._username    = fld["Entity Name"].value;
  370.  
  371.       // get the group list
  372.       var tAssign = new Query();
  373.       tAssign.database = this._database;
  374.  
  375.       // Use tEntity for group descriptions
  376.       tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' + SM_ENTITY_GROUP ;
  377.       tEntity.active = true;
  378.  
  379.       // Get the group list first
  380.       tAssign.sql='select * from smassign sm where sm."Child" = "' +
  381.                   username+'" and sm."Assign Type"='+ SM_ASSIGN_GROUP_USER;
  382.       var fldAssign = tAssign.rowset.fields;
  383.  
  384.       tAssign.active = true;
  385.       while (! tAssign.rowset.endOfSet) {
  386.          if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + fldAssign["Parent"].value + "'"))
  387.             this._group[fldAssign["Parent"].value] = fld["Description"].value;
  388.          tAssign.rowset.next();
  389.       }
  390.  
  391.       // now use tEntity for resource descriptions
  392.       tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' + SM_ENTITY_RESOURCE ;
  393.       tEntity.active = true;
  394.  
  395.       // Get the resource list for this user
  396.       tAssign.sql='select * from smassign sm where sm."Child" = "' +
  397.                   username+'" and sm."Assign Type"='+ SM_ASSIGN_RESOURCE_USER;
  398.       tAssign.active = true;
  399.       while (! tAssign.rowset.endOfSet) {
  400.          if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + fldAssign["Parent"].value + "'"))
  401.             this._resource[fldAssign["Parent"].value] = fld["Description"].value;
  402.          tAssign.rowset.next();
  403.       }
  404.  
  405.       // get the resource list for group assignments
  406.       tAssign.sql='select * from smassign sm where sm."Assign Type"='
  407.                  + SM_ASSIGN_RESOURCE_GROUP;
  408.       tAssign.active = true;
  409.       var i=0;
  410.       var group="";
  411.       for (i=0; i<this._group.count(); i++) {
  412.          group = ( i==0 ) ? this._group.firstKey : this._group.nextKey(group);
  413.          tAssign.rowset.filter = "Child='" + group + "'";
  414.          tAssign.rowset.first();
  415.  
  416.          while (!tAssign.rowset.endOfSet) {
  417.             if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + fldAssign["Parent"].value + "'"))
  418.                this._resource[fldAssign["Parent"].value] = fld["Description"].value;
  419.             tAssign.rowset.next();
  420.          }
  421.       }
  422.  
  423.       // set the administrator flag if this user is
  424.       // a member of the administrators group
  425.       this._admin = this.isMemberOf('ADMINISTRATORS');
  426.  
  427.       return (true);
  428.    }
  429.  
  430. }
  431.  
  432. /***************************************************************************\
  433. *                                                                           *
  434. * Classname   SecurityManagerAdmin                                          *
  435. *                                                                           *
  436. * Purpose     Subclass of SecurityManager with additional methods for       *
  437. *             security system administration.                               *
  438. *                                                                           *
  439. * Properties:                                                               *
  440. *                                                                           *
  441. *    classname - "SecurityManagerAdmin"                                     *
  442. *                                                                           *
  443. * Additional Methods                                                        *
  444. *                                                                           *
  445. *   assignResourceGroup( <resource name>, <group name> )                    *
  446. *   assignResourceUser( <resource name>, <user name> )                      *
  447. *   assignGroupUser( <group name>, <user name> )                            *
  448. *   createGroup( <SmGroup object> )       // see getGroupObject()           *
  449. *   createPolicy( <SmPolicy object> )     // see getPolicyObject()          *
  450. *   createResource( <SmResource object> ) // see getResourceObject()        *
  451. *   createUser( <SmUser object> )         // see getUserObject()            *
  452. *   deleteGroup( <group name> )                                             *
  453. *   deletePolicy( <policy name> )                                           *
  454. *   deleteResource( <resource name> )                                       *
  455. *   deleteUser( <user name> )                                               *
  456. *   getAllGroups( [<related name>, <related type>] )   //returns AssocArray *
  457. *   getAllPolicies()                                   //returns AssocArray *
  458. *   getAllResources( [<related name>, related type>] ) //returns AssocArray *
  459. *   getAllUsers( [<related name>, related type>] )     //returns AssocArray *
  460. *   getGroupObject( [<group name>] )          //returns SmGroup object      *
  461. *   getPolicyObject( [<policy name>] )        //returns SmPolicy object     *
  462. *   getResourceObject( [<resource name>] )    //returns SmResource object   *
  463. *   getUserObject( [<user name>] )            //returns SmUser object       *
  464. *   unassignResourceGroup( <resource name>, <group name> )                  *
  465. *   unassignResourceUser( <resource name>, <user name> )                    *
  466. *   unassignGroupUser( <group name>, <user name> )                          *
  467. *   updateGroup( <SmGroup object> )         // see getGroupObject()         *
  468. *   updatePolicy( <SmPolicy object> )       // see getPolicyObject()        *
  469. *   updateResource( <SmResource object> )   // see getResourceObject()      *
  470. *   updateUser( <SmUser object> )           // see getUserObject()          *
  471. *                                                                           *
  472. *   Unless otherwise indicated above, methods that complete successfully    *
  473. *   return a true value. Methods that do not complete successfully, throw   *
  474. *   an exception of class SmException. See example.                         *
  475. *                                                                           *
  476. *   The getAllXxxxx methods take two optional parameters. Without these     *
  477. *   parameters an array is returned that contains all of the requested      *
  478. *   entities. If the two parameters are passed, then the array contains     *
  479. *   only those entities that are related to the parameters. For instance    *
  480. *   you can retrieve a list of users who are members of a group named       *
  481. *   "administrators" with this call:                                        *
  482. *                                                                           *
  483. *   var x = getAllUsers( "Administrators", SM_ENTITY_GROUP )                *
  484. *                                                                           *
  485. *   The second parameter defines the type of the first. In this case it     *
  486. *   is a group. The entity types are defined in SECURITY.H. The are:        *
  487. *                                                                           *
  488. *           SM_ENTITY_GROUP                                                 *
  489. *           SM_ENTITY_POLICY                                                *
  490. *           SM_ENTITY_RESOURCE                                              *
  491. *           SM_ENTITY_USER                                                  *
  492. *                                                                           *
  493. *   The getXxxxxObject methods return an object of the specified type.      *
  494. *   This object's properties can then be queried or set. The objects        *
  495. *   can then be passed to the createXxxxx and updateXxxxx methods. The      *
  496. *   members of the different objects are:                                   *
  497. *                                                                           *
  498. *   SmGroup.created           // date create (read only)                    *
  499. *   SmGroup.description       // up to 80 characters                        *
  500. *   SmGroup.name              // up to 20 characters                        *
  501. *                                                                           *
  502. *   SmPolicy.boolean          // true if value is boolean, false if numeric *
  503. *   SmPolicy.created                                                        *
  504. *   SmPolicy.description                                                    *
  505. *   SmPolicy.name                                                           *
  506. *   SmPolicy.value            // boolean or numeric value                   *
  507. *                                                                           *
  508. *   SmResource.created                                                      *
  509. *   SmResource.description                                                  *
  510. *   SmResource.name                                                         *
  511. *                                                                           *
  512. *   SmUser.created                                                          *
  513. *   SmUser.description                                                      *
  514. *   SmUser.disabled          // boolean                                     *
  515. *   SmUser.login             // date of last successful login (read only)   *
  516. *   SmUser.lockout           // boolean                                     *
  517. *   SmUser.name                                                             *
  518. *   SmUser.password          // getUserObject() sets this to null           *
  519. *                                                                           *
  520. \***************************************************************************/
  521. class SecurityManagerAdmin extends SecurityManager {
  522.    this.classname    = "SecurityManagerAdmin";
  523.    this._admin       = false;
  524.  
  525.    function assignGroupUser(groupName, userName) {
  526.       // check for error conditions
  527.       if (!this._admin)
  528.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  529.       if (groupName == null)
  530.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  531.       if (userName == null)
  532.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  533.  
  534.       // convert names to uppercase
  535.       var gName = groupName.toUpperCase();
  536.       var uName = userName.toUpperCase();
  537.  
  538.       // confirm these are real names
  539.       var tEntity = new Query();
  540.       tEntity.database = this._database;
  541.       tEntity.sql = 'select * from smentity';
  542.       tEntity.active = true;
  543.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + gName + "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  544.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  545.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + uName + "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  546.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  547.  
  548.       // check for duplicate assignment
  549.       var tAssign = new Query();
  550.       tAssign.database = this._database;
  551.       tAssign.sql = 'select * from smassign';
  552.       tAssign.active = true;
  553.       if (!tAssign.rowset.applyLocate("Parent = '" + gName + "' and Child = '" + uName + "' and " + '"Assign Type" = ' + SM_ASSIGN_GROUP_USER)) {
  554.          tAssign.rowset.beginAppend();
  555.          tAssign.rowset.fields["Parent"].value      = gName;
  556.          tAssign.rowset.fields["Child"].value       = uName;
  557.          tAssign.rowset.fields["Assign Type"].value = SM_ASSIGN_GROUP_USER;
  558.          tAssign.rowset.save();
  559.       }
  560.       return (true);
  561.    }
  562.  
  563.    function assignResourceGroup(resourceName, groupName) {
  564.       // check for error conditions
  565.       if (!this._admin)
  566.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  567.       if (resourceName == null)
  568.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  569.       if (groupName == null)
  570.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  571.  
  572.       // convert names to uppercase
  573.       var rName = resourceName.toUpperCase();
  574.       var gName = groupName.toUpperCase();
  575.  
  576.       // confirm these are real names
  577.       var tEntity = new Query();
  578.       tEntity.database = this._database;
  579.       tEntity.sql = 'select * from smentity';
  580.       tEntity.active = true;
  581.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + rName + "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  582.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);    
  583.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + gName + "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  584.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  585.  
  586.       // check for duplicate assignment
  587.       var tAssign = new Query();
  588.       tAssign.database = this._database;
  589.       tAssign.sql = 'select * from smassign';
  590.       tAssign.active = true;
  591.       if (!tAssign.rowset.applyLocate("Parent = '" + rName + "' and Child = '" + gName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_GROUP)) {
  592.          tAssign.rowset.beginAppend();
  593.          tAssign.rowset.fields["Parent"].value      = rName;
  594.          tAssign.rowset.fields["Child"].value       = gName;
  595.          tAssign.rowset.fields["Assign Type"].value = SM_ASSIGN_RESOURCE_GROUP;
  596.          tAssign.rowset.save();
  597.       }
  598.       return (true);
  599.    }
  600.  
  601.    function assignResourceUser(resourceName, userName) {
  602.       // check for error conditions
  603.       if (!this._admin)
  604.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  605.       if (resourceName == null)
  606.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  607.       if (userName == null)
  608.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  609.  
  610.       // convert names to uppercase
  611.       var rName = resourceName.toUpperCase();
  612.       var uName = userName.toUpperCase();
  613.  
  614.       // confirm these are real names
  615.       var tEntity = new Query();
  616.       tEntity.database = this._database;
  617.       tEntity.sql = 'select * from smentity';
  618.       tEntity.active = true;
  619.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + rName + "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  620.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  621.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + uName + "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  622.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  623.  
  624.       // check for duplicate assignment
  625.       var tAssign = new Query();
  626.       tAssign.database = this._database;
  627.       tAssign.sql = 'select * from smassign';
  628.       tAssign.active = true;
  629.       if (!tAssign.rowset.applyLocate("Parent = '" + rName + "' and Child = '" + uName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_USER)) {
  630.          tAssign.rowset.beginAppend();
  631.          tAssign.rowset.fields["Parent"].value      = rName;
  632.          tAssign.rowset.fields["Child"].value       = uName;
  633.          tAssign.rowset.fields["Assign Type"].value = SM_ASSIGN_RESOURCE_USER;
  634.          tAssign.rowset.save();
  635.       }
  636.       return (true);
  637.    }
  638.  
  639.    function createGroup(newGroup) {
  640.       // check for error conditions
  641.       if (!this._admin)
  642.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  643.       if (newGroup.name == null)
  644.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  645.  
  646.       // convert group name to uppercase
  647.       gName = newGroup.name.toUpperCase();
  648.  
  649.       var tEntity = new Query();
  650.       tEntity.database = this._database;
  651.       tEntity.sql = 'select * from smentity';
  652.       tEntity.active = true;
  653.  
  654.       // throw exception on duplicate record
  655.       if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + gName + "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  656.          throw new SmException(SM_ERROR_DUPLICATE_GROUPNAME);
  657.  
  658.       // add this new group
  659.       tEntity.rowset.beginAppend();
  660.       tEntity.rowset.fields["Created"].value     = new Date();
  661.       tEntity.rowset.fields["Description"].value = newGroup.description;
  662.       tEntity.rowset.fields["Entity Name"].value = gName;
  663.       tEntity.rowset.fields["Entity Type"].value = SM_ENTITY_GROUP;
  664.       tEntity.rowset.save();
  665.  
  666.       return (true);
  667.    }
  668.  
  669.    function createPolicy(newPolicy) {
  670.       // check for error conditions
  671.       if (!this._admin)
  672.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  673.       if (newPolicy.name == null)
  674.          throw new SmException(SM_ERROR_INVALID_POLICYNAME);
  675.  
  676.       // convert policy name to uppercase
  677.       pName = newPolicy.name.toUpperCase();
  678.  
  679.       var tEntity = new Query();
  680.       tEntity.database = this._database;
  681.       tEntity.sql = 'select * from smentity';
  682.       tEntity.active = true;
  683.  
  684.       // throw exception on duplicate record
  685.       if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + pName + "'" + ' and "Entity Type" = ' + SM_ENTITY_POLICY))
  686.          throw new SmException(SM_ERROR_DUPLICATE_POLICYNAME);
  687.  
  688.       // add this new policy
  689.       tEntity.rowset.beginAppend();
  690.       tEntity.rowset.fields["Created"].value         = new Date();
  691.       tEntity.rowset.fields["Description"].value     = newPolicy.description;
  692.       tEntity.rowset.fields["Entity Name"].value     = pName;
  693.       tEntity.rowset.fields["Entity Type"].value     = SM_ENTITY_POLICY;
  694.       tEntity.rowset.fields["Policy Boolean"].value  = newPolicy.boolean;
  695.       tEntity.rowset.fields["Policy Value"].value    = 
  696.          (newPolicy.boolean ? (newPolicy.value ? 1 : 0) : newPolicy.value);
  697.       tEntity.rowset.save();
  698.  
  699.       return (true);
  700.    }
  701.  
  702.    function createResource(newResource) {
  703.       // check for error conditions
  704.       if (!this._admin)
  705.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  706.       if (newResource.name == null)
  707.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  708.  
  709.       // convert resource name to uppercase
  710.       rName = newResource.name.toUpperCase();
  711.  
  712.       var tEntity = new Query();
  713.       tEntity.database = this._database;
  714.       tEntity.sql = 'select * from smentity'
  715.       tEntity.active = true;
  716.  
  717.       // throw exception on duplicate record
  718.       if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + rName + "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  719.          throw new SmException(SM_ERROR_DUPLICATE_RESOURCENAME);
  720.  
  721.       // add this new resource
  722.       tEntity.rowset.beginAppend();
  723.       tEntity.rowset.fields["Created"].value         = new Date();
  724.       tEntity.rowset.fields["Entity Name"].value = rName;
  725.       tEntity.rowset.fields["Entity Type"].value = SM_ENTITY_RESOURCE;
  726.       tEntity.rowset.fields["Description"].value = newResource.description;
  727.       tEntity.rowset.save();
  728.  
  729.       return (true);
  730.    }
  731.  
  732.    function createUser(newUser) {
  733.       // check for error conditions
  734.       if (!this._admin)
  735.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  736.       if (newUser.name == null)
  737.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  738.  
  739.       // convert user name to uppercase
  740.       uName = newUser.name.toUpperCase();
  741.  
  742.       var tEntity = new Query();
  743.       tEntity.database = this._database;
  744.       tEntity.sql = 'select * from smentity';
  745.       tEntity.active = true;
  746.  
  747.       // throw exception on duplicate record
  748.       if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + uName + "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  749.          throw new SmException(SM_ERROR_DUPLICATE_USERNAME);
  750.  
  751.       // add this new user
  752.       tEntity.rowset.beginAppend();
  753.       tEntity.rowset.fields["Created"].value       = new Date();
  754.       tEntity.rowset.fields["Entity Name"].value   = uName;
  755.       tEntity.rowset.fields["Description"].value   = newUser.description;
  756.       tEntity.rowset.fields["User Password"].value = newUser.password;
  757.       tEntity.rowset.fields["User Disabled"].value = newUser.disabled;
  758.       tEntity.rowset.fields["User Lockout"].value  = newUser.lockout;
  759.       tEntity.rowset.fields["Entity Type"].value   = SM_ENTITY_USER;
  760.       tEntity.rowset.save();
  761.  
  762.       return (true);
  763.    }
  764.  
  765.    function deleteGroup(groupName) {
  766.       // check for error conditions
  767.       if (!this._admin)
  768.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  769.       if (groupName == null)
  770.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  771.  
  772.       // convert name to uppercase
  773.       var gName = groupName.toUpperCase();
  774.  
  775.       // see if the group exists
  776.       var tEntity = new Query();
  777.       tEntity.database = this._database;
  778.       tEntity.sql = 'select * from smentity';
  779.       tEntity.active = true;
  780.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + gName + "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  781.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  782.  
  783.       // See if this entity is locked
  784.       if (tEntity.rowset.fields["Entity Lock"].value)
  785.          throw new SmException(SM_ERROR_CAN_NOT_DELETE_SYSTEM_ENTITY);
  786.  
  787.       // OK, delete this group and all user/resource assignments
  788.       tEntity.database.beginTrans();
  789.       try {
  790.          tEntity.rowset.delete();
  791.  
  792.          var tAssign = new Query();
  793.          tAssign.database = this._database;
  794.          tAssign.sql = 'select * from smassign';
  795.          tAssign.active = true;
  796.  
  797.          // delete user assignments
  798.          while (tAssign.rowset.applyLocate("Parent = '" + gName + "' and " + '"Assign Type" = ' + SM_ASSIGN_GROUP_USER))
  799.             tAssign.rowset.delete();
  800.  
  801.          // delete resource assignments
  802.          while (tAssign.rowset.applyLocate("Child = '" + gName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_GROUP))
  803.             tAssign.rowset.delete();
  804.  
  805.          // commit the deletes
  806.          tEntity.database.commit();
  807.       }
  808.       catch (Exception e) {
  809.          tEntity.database.rollback();
  810.          throw e;
  811.       }
  812.       return (true);
  813.    }
  814.  
  815.    function deletePolicy(policyName) {
  816.       // check for error conditions
  817.       if (!this._admin)
  818.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  819.       if (policyName == null)
  820.          throw new SmException(SM_ERROR_INVALID_POLICYNAME);
  821.  
  822.       // convert name to uppercase
  823.       var pName = policyName.toUpperCase();
  824.  
  825.       // see if the resource exists
  826.       var tEntity = new Query();
  827.       tEntity.database = this._database;
  828.       tEntity.sql = 'select * from smentity';
  829.       tEntity.active = true;
  830.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + pName + "'" + ' and "Entity Type" = ' + SM_ENTITY_POLICY))
  831.          throw new SmException(SM_ERROR_INVALID_POLICYNAME);
  832.  
  833.       // See if this entity is locked
  834.       if (tEntity.rowset.fields["Entity Lock"].value)
  835.          throw new SmException(SM_ERROR_CAN_NOT_DELETE_SYSTEM_ENTITY);
  836.  
  837.       // OK, delete this policy and all user/resource assignments
  838.       tEntity.database.beginTrans();
  839.       try {
  840.          tEntity.rowset.delete();
  841.          // commit the delete
  842.          tEntity.database.commit();
  843.       }
  844.       catch (Exception e) {
  845.          tEntity.database.rollback();
  846.          throw e;
  847.       }
  848.       return (true);
  849.    }
  850.  
  851.    function deleteResource(resourceName) {
  852.       // check for error conditions
  853.       if (!this._admin)
  854.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  855.       if (resourceName == null)
  856.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  857.  
  858.       // convert name to uppercase
  859.       var rName = resourceName.toUpperCase();
  860.  
  861.       // see if the resource exists
  862.       var tEntity = new Query();
  863.       tEntity.database = this._database;
  864.       tEntity.sql = 'select * from smentity';
  865.       tEntity.active = true;
  866.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + rName + "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  867.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  868.  
  869.       // See if this entity is locked
  870.       if (tEntity.rowset.fields["Entity Lock"].value)
  871.          throw new SmException(SM_ERROR_CAN_NOT_DELETE_SYSTEM_ENTITY);
  872.  
  873.       // OK, delete this resource and all group/user assignments
  874.       tEntity.database.beginTrans();
  875.       try {
  876.          tEntity.rowset.delete();
  877.  
  878.          var tAssign = new Query();
  879.          tAssign.database = this._database;
  880.          tAssign.sql = 'select * from smassign';
  881.          tAssign.active = true;
  882.  
  883.          // delete group assignments
  884.          while (tAssign.rowset.applyLocate("Parent = '" + rName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_GROUP))
  885.             tAssign.rowset.delete();
  886.  
  887.          // delete user assignments
  888.          while (tAssign.rowset.applyLocate("Parent = '" + rName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_USER))
  889.             tAssign.rowset.delete();
  890.  
  891.          // commit the deletes
  892.          tEntity.database.commit();
  893.       }
  894.       catch (Exception e) {
  895.          tEntity.database.rollback();
  896.          throw e;
  897.       }
  898.       return (true);
  899.    }
  900.  
  901.    function deleteUser(userName) {
  902.       // check for error conditions
  903.       if (!this._admin)
  904.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  905.       if (userName == null)
  906.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  907.  
  908.       // convert names to uppercase
  909.       var uName = userName.toUpperCase();
  910.  
  911.       // can't delete current user
  912.       if (uName == this._username)
  913.          throw new SmException(SM_ERROR_CAN_NOT_DELETE_CURRENT_USER);
  914.  
  915.       // see if the user exists
  916.       var tEntity = new Query();
  917.       tEntity.database = this._database;
  918.       tEntity.sql = 'select * from smentity';
  919.       tEntity.active = true;
  920.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + uName + "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  921.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  922.  
  923.       // See if this entity is locked
  924.       if (tEntity.rowset.fields["Entity Lock"].value)
  925.          throw new SmException(SM_ERROR_CAN_NOT_DELETE_SYSTEM_ENTITY);
  926.  
  927.       // OK, delete this user and all group/resource assignments
  928.       tEntity.database.beginTrans();
  929.       try {
  930.          tEntity.rowset.delete();
  931.  
  932.          var tAssign = new Query();
  933.          tAssign.database = this._database;
  934.          tAssign.sql = 'select * from smassign';
  935.          tAssign.active = true;
  936.  
  937.          // delete group assignments
  938.          while (tAssign.rowset.applyLocate("Child = '" + uName + "' and " + '"Assign Type" = ' + SM_ASSIGN_GROUP_USER))
  939.             tAssign.rowset.delete();
  940.  
  941.          // delete resource assignments
  942.          while (tAssign.rowset.applyLocate("Child = '" + uName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_USER))
  943.             tAssign.rowset.delete();
  944.  
  945.          // commit the deletes
  946.          tEntity.database.commit();
  947.       }
  948.       catch (Exception e) {
  949.          tEntity.database.rollback();
  950.          throw e;
  951.       }
  952.       return (true);
  953.    }
  954.  
  955.    function getAllGroups(entityName, entityType) {
  956.       var eArray = new AssocArray();
  957.  
  958.       // check for error conditions
  959.       if (!this._admin)
  960.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  961.  
  962.       // see if the group list is filtered
  963.       if (("" + entityType) == "false") {
  964.          // no second param, throw exception if only one parameter passed
  965.          if ("" + entityName != "false")
  966.             throw new SmException(SM_ERROR_INVALID_ENTITY_TYPE);
  967.  
  968.          // No filter, return all groups
  969.          var tEntity = new Query();
  970.          tEntity.database = this._database;
  971.          tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  972.                     + SM_ENTITY_GROUP ;
  973.          tEntity.active = true;
  974.  
  975.          while (!tEntity.rowset.endOfSet) {
  976.             eArray[tEntity.rowset.fields["Entity Name"].value] = 
  977.                    tEntity.rowset.fields["Description"].value;
  978.             tEntity.rowset.next();
  979.          }
  980.       }
  981.       else {
  982.          // convert to upper case
  983.          var eName = entityName.toUpperCase();
  984.  
  985.          // store error code in case needed
  986.          switch (entityType) {
  987.          case SM_ENTITY_USER:
  988.             var errorInvalid = SM_ERROR_INVALID_USERNAME;
  989.             var sql = "sm.Child = '" + eName + "'" + ' and sm."Assign Type" = ' + SM_ASSIGN_GROUP_USER;
  990.             break;
  991.          case SM_ENTITY_RESOURCE:
  992.             var errorInvalid = SM_ERROR_INVALID_RESOURCENAME;
  993.             var sql = "sm.Parent = '" + eName + "'" + ' and sm."Assign Type" = ' + SM_ASSIGN_RESOURCE_GROUP;
  994.             break;
  995.          default:
  996.             throw new SmException(SM_ERROR_INVALID_ENTITY_TYPE);
  997.          }
  998.          
  999.          // passed an entity name/type pair, restrict list to this entity
  1000.          if (entityName == null)
  1001.             throw new SmException(errorInvalid);
  1002.  
  1003.          // see if entity exists
  1004.          var tEntity = new Query();
  1005.          tEntity.database = this._database;
  1006.          tEntity.sql = 'select * from smentity';
  1007.          tEntity.active = true;
  1008.          if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + eName + "'" + ' and "Entity Type" = ' + entityType))
  1009.             throw new SmException(errorInvalid);
  1010.  
  1011.          // now use tEntity to lookup description
  1012.          tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  1013.                     + SM_ENTITY_GROUP ;
  1014.          tEntity.active = true;
  1015.  
  1016.          // find the group assignments
  1017.          var tAssign = new Query();
  1018.          tAssign.database = this._database;
  1019.          tAssign.sql = 'select * from smassign sm where ' + sql;
  1020.          tAssign.active = true;
  1021.  
  1022.          while (!tAssign.rowset.endOfSet) {
  1023.             if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + 
  1024.                     ((entityType == SM_ENTITY_USER) ? 
  1025.                     tAssign.rowset.fields['Parent'].value :
  1026.                     tAssign.rowset.fields['Child'].value) + "'")) {
  1027.  
  1028.                eArray[tEntity.rowset.fields["Entity Name"].value] = 
  1029.                       tEntity.rowset.fields["Description"].value;
  1030.  
  1031.             }
  1032.             tAssign.rowset.next();
  1033.          }
  1034.  
  1035.  
  1036.       }
  1037.       return (eArray);
  1038.    }
  1039.  
  1040.    function getAllPolicies() {
  1041.       var eArray = new AssocArray();
  1042.  
  1043.       // check for error conditions
  1044.       if (!this._admin)
  1045.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1046.  
  1047.       // view just the current policy entries
  1048.       var tEntity = new Query();
  1049.       tEntity.database = this._database;
  1050.       tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  1051.                     + SM_ENTITY_POLICY ;
  1052.       tEntity.active = true;
  1053.  
  1054.       // store policies to pArray
  1055.       while (!tEntity.rowset.endOfSet) {
  1056.          eArray[tEntity.rowset.fields["Entity Name"].value] = 
  1057.                 tEntity.rowset.fields["Description"].value;
  1058.          tEntity.rowset.next();
  1059.       }
  1060.       return (eArray);
  1061.    }
  1062.  
  1063.    function getAllResources(entityName, entityType) {
  1064.       var eArray = new AssocArray();
  1065.  
  1066.       // check for error conditions
  1067.       if (!this._admin)
  1068.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1069.  
  1070.       // see if the resource list is filtered
  1071.       if (("" + entityType) == "false") {
  1072.          // no second param, throw exception if only one parameter passed
  1073.          if ("" + entityName != "false")
  1074.             throw new SmException(SM_ERROR_INVALID_ENTITY_TYPE);
  1075.  
  1076.          // No filter, return all resources
  1077.          var tEntity = new Query();
  1078.          tEntity.database = this._database;
  1079.          tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  1080.                     + SM_ENTITY_RESOURCE ;
  1081.          tEntity.active = true;
  1082.  
  1083.          while (!tEntity.rowset.endOfSet) {
  1084.             eArray[tEntity.rowset.fields["Entity Name"].value] =
  1085.                    tEntity.rowset.fields["Description"].value;
  1086.             tEntity.rowset.next();
  1087.          }
  1088.       }
  1089.       else {
  1090.          // convert to upper case
  1091.          var eName = entityName.toUpperCase();
  1092.  
  1093.          // store error code in case needed
  1094.          switch (entityType) {
  1095.          case SM_ENTITY_USER:
  1096.             var errorInvalid = SM_ERROR_INVALID_USERNAME;
  1097.             var sql = "sm.Child = '" + eName + "'" + ' and sm."Assign Type" = ' + SM_ASSIGN_RESOURCE_USER;
  1098.             break;
  1099.          case SM_ENTITY_GROUP:
  1100.             var errorInvalid = SM_ERROR_INVALID_GROUPNAME;
  1101.             var sql = "sm.Child = '" + eName + "'" + ' and sm."Assign Type" = ' + SM_ASSIGN_RESOURCE_GROUP;
  1102.             break;
  1103.          default:
  1104.             throw new SmException(SM_ERROR_INVALID_ENTITY_TYPE);
  1105.          }
  1106.          
  1107.          // passed an entity name/type pair, restrict list to this entity
  1108.          if (entityName == null)
  1109.             throw new SmException(errorInvalid);
  1110.  
  1111.          // see if entity exists
  1112.          var tEntity = new Query();
  1113.          tEntity.database = this._database;
  1114.          tEntity.sql = 'select * from smentity';
  1115.          tEntity.active = true;
  1116.          if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + eName + "'" + ' and "Entity Type" = ' + entityType))
  1117.             throw new SmException(errorInvalid);
  1118.  
  1119.          // now use tEntity to lookup description
  1120.          tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  1121.                     + SM_ENTITY_RESOURCE ;
  1122.          tEntity.active = true;
  1123.  
  1124.          // find the resource assignments
  1125.          var tAssign = new Query();
  1126.          tAssign.database = this._database;
  1127.          tAssign.sql = 'select * from smassign sm where ' + sql;
  1128.          tAssign.active = true;
  1129.  
  1130.          while (!tAssign.rowset.endOfSet) {
  1131.             // When looking for resource pairs, the resource
  1132.             // name is always the parent
  1133.             if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + 
  1134.                     tAssign.rowset.fields['Parent'].value + "'")) {
  1135.                eArray[tEntity.rowset.fields["Entity Name"].value] = 
  1136.                       tEntity.rowset.fields["Description"].value;
  1137.             }
  1138.             tAssign.rowset.next();
  1139.          }
  1140.       }
  1141.       return (eArray);
  1142.    }
  1143.  
  1144.    function getAllUsers(entityName, entityType) {
  1145.       var eArray = new AssocArray();
  1146.  
  1147.       // check for error conditions
  1148.       if (!this._admin)
  1149.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1150.  
  1151.       // see if the user list is filtered
  1152.       if (("" + entityType) == "false") {
  1153.          // no second param, throw exception if only one parameter passed
  1154.          if ("" + entityName != "false")
  1155.             throw new SmException(SM_ERROR_INVALID_ENTITY_TYPE);
  1156.  
  1157.          // No filter, return all users
  1158.          var tEntity = new Query();
  1159.          tEntity.database = this._database;
  1160.          tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  1161.                     + SM_ENTITY_USER ;
  1162.          tEntity.active = true;
  1163.  
  1164.          while (!tEntity.rowset.endOfSet) {
  1165.             eArray[tEntity.rowset.fields["Entity Name"].value] = 
  1166.                    tEntity.rowset.fields["Description"].value;
  1167.             tEntity.rowset.next();
  1168.          }
  1169.       }
  1170.       else {
  1171.          // convert to upper case
  1172.          var eName = entityName.toUpperCase();
  1173.  
  1174.          // store error code in case needed
  1175.          switch (entityType) {
  1176.          case SM_ENTITY_RESOURCE:
  1177.             var errorInvalid = SM_ERROR_INVALID_RESOURCENAME;
  1178.             var sql = "sm.Parent = '" + eName + "'" + ' and sm."Assign Type" = ' + SM_ASSIGN_RESOURCE_USER;
  1179.             break;
  1180.          case SM_ENTITY_GROUP:
  1181.             var errorInvalid = SM_ERROR_INVALID_GROUPNAME;
  1182.             var sql = "sm.Parent = '" + eName + "'" + ' and sm."Assign Type" = ' + SM_ASSIGN_GROUP_USER;
  1183.             break;
  1184.          default:
  1185.             throw new SmException(SM_ERROR_INVALID_ENTITY_TYPE);
  1186.          }
  1187.          
  1188.          // passed an entity name/type pair, restrict list to this entity
  1189.          if (entityName == null)
  1190.             throw new SmException(errorInvalid);
  1191.  
  1192.          // see if entity exists
  1193.          var tEntity = new Query();
  1194.          tEntity.database = this._database;
  1195.          tEntity.sql = 'select * from smentity';
  1196.          tEntity.active = true;
  1197.          if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + eName + "'" + ' and "Entity Type" = ' + entityType))
  1198.             throw new SmException(errorInvalid);
  1199.  
  1200.          // now use tEntity to lookup description
  1201.          tEntity.sql = 'select * from smentity sm where sm."Entity Type" = ' 
  1202.                     + SM_ENTITY_USER ;
  1203.          tEntity.active = true;
  1204.  
  1205.          // find the user assignments
  1206.          var tAssign = new Query();
  1207.          tAssign.database = this._database;
  1208.          tAssign.sql = 'select * from smassign sm where ' + sql;
  1209.          tAssign.active = true;
  1210.  
  1211.          while (!tAssign.rowset.endOfSet) {
  1212.             // When looking for user pairs, the user
  1213.             // name is always the child
  1214.             if (tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + 
  1215.                     tAssign.rowset.fields['Child'].value + "'")) {
  1216.                eArray[tEntity.rowset.fields["Entity Name"].value] = 
  1217.                       tEntity.rowset.fields["Description"].value;
  1218.             }
  1219.             tAssign.rowset.next();
  1220.          }
  1221.       }
  1222.       return (eArray);
  1223.    }
  1224.  
  1225.    function getGroupObject( entityName ) {
  1226.       // check for error conditions
  1227.       if (!this._admin)
  1228.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1229.  
  1230.       var entityObj = new SmGroup();
  1231.  
  1232.       // see if parameter passed
  1233.       if (("" + entityName) != "false") {
  1234.          // convert name to uppercase
  1235.          eName = entityName.toUpperCase();
  1236.  
  1237.          var tEntity = new Query();
  1238.          tEntity.database = this._database;
  1239.          tEntity.sql = 'select * from smentity';
  1240.          tEntity.active = true;
  1241.  
  1242.          // throw exception on missing record
  1243.          if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + eName + 
  1244.                           "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  1245.             throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  1246.  
  1247.          // set properties of object for this entity
  1248.          entityObj.created     = tEntity.rowset.fields["Created"].value;
  1249.          entityObj.description = tEntity.rowset.fields["Description"].value;
  1250.          entityObj.name        = tEntity.rowset.fields["Entity Name"].value;
  1251.       }
  1252.       
  1253.       return (entityObj);
  1254.    }
  1255.  
  1256.    function getPolicyObject( entityName ) {
  1257.       // check for error conditions
  1258.       if (!this._admin)
  1259.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1260.  
  1261.       var entityObj = new SmPolicy();
  1262.  
  1263.       // see if parameter passed
  1264.       if (("" + entityName) != "false") {
  1265.          // convert name to uppercase
  1266.          eName = entityName.toUpperCase();
  1267.  
  1268.          var tEntity = new Query();
  1269.          tEntity.database = this._database;
  1270.          tEntity.sql = 'select * from smentity';
  1271.          tEntity.active = true;
  1272.  
  1273.          // throw exception on missing record
  1274.          if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + eName + 
  1275.                           "'" + ' and "Entity Type" = ' + SM_ENTITY_POLICY))
  1276.             throw new SmException(SM_ERROR_INVALID_POLICYNAME);
  1277.  
  1278.          // set properties of object for this entity
  1279.          entityObj.boolean     = tEntity.rowset.fields["Policy Boolean"].value;
  1280.          entityObj.created     = tEntity.rowset.fields["Created"].value;
  1281.          entityObj.description = tEntity.rowset.fields["Description"].value;
  1282.          entityObj.name        = tEntity.rowset.fields["Entity Name"].value;
  1283.          entityObj.value = (entityObj.boolean ?
  1284.                            (tEntity.rowset.fields["Policy Value"].value != 0) :
  1285.                            tEntity.rowset.fields["Policy Value"].value);
  1286.       }
  1287.       
  1288.       return (entityObj);
  1289.    }
  1290.  
  1291.    function getResourceObject( entityName ) {
  1292.       // check for error conditions
  1293.       if (!this._admin)
  1294.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1295.  
  1296.       var entityObj = new SmResource();
  1297.  
  1298.       // see if parameter passed
  1299.       if (("" + entityName) != "false") {
  1300.          // convert name to uppercase
  1301.          eName = entityName.toUpperCase();
  1302.  
  1303.          var tEntity = new Query();
  1304.          tEntity.database = this._database;
  1305.          tEntity.sql = 'select * from smentity';
  1306.          tEntity.active = true;
  1307.  
  1308.          // throw exception on missing record
  1309.          if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + eName + 
  1310.                           "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  1311.             throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  1312.  
  1313.          // set properties of object for this entity
  1314.          entityObj.created     = tEntity.rowset.fields["Created"].value;
  1315.          entityObj.description = tEntity.rowset.fields["Description"].value;
  1316.          entityObj.name        = tEntity.rowset.fields["Entity Name"].value;
  1317.       }
  1318.       
  1319.       return (entityObj);
  1320.    }
  1321.  
  1322.    function getUserObject( entityName ) {
  1323.       // check for error conditions
  1324.       if (!this._admin)
  1325.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1326.  
  1327.       var entityObj = new SmUser();
  1328.  
  1329.       // see if parameter passed
  1330.       if (("" + entityName) != "false") {
  1331.          // convert name to uppercase
  1332.          eName = entityName.toUpperCase();
  1333.  
  1334.          var tEntity = new Query();
  1335.          tEntity.database = this._database;
  1336.          tEntity.sql = 'select * from smentity';
  1337.          tEntity.active = true;
  1338.  
  1339.          // throw exception on missing record
  1340.          if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + eName + 
  1341.                           "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  1342.             throw new SmException(SM_ERROR_INVALID_USERNAME);
  1343.  
  1344.          // set properties of object for this entity
  1345.          entityObj.created     = tEntity.rowset.fields["Created"].value;
  1346.          entityObj.description = tEntity.rowset.fields["Description"].value;
  1347.          entityObj.disabled    = tEntity.rowset.fields["User Disabled"].value;
  1348.          entityObj.login       = tEntity.rowset.fields["User Login"].value;
  1349.          entityObj.lockout     = tEntity.rowset.fields["User Lockout"].value;
  1350.          entityObj.name        = tEntity.rowset.fields["Entity Name"].value;
  1351.          entityObj.password    = null;
  1352.       }
  1353.       
  1354.       return (entityObj);
  1355.    }
  1356.  
  1357.    function unassignGroupUser(groupName, userName) {
  1358.       // check for error conditions
  1359.       if (!this._admin)
  1360.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1361.       if (groupName == null)
  1362.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  1363.       if (userName == null)
  1364.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  1365.  
  1366.       // convert names to uppercase
  1367.       var gName = groupName.toUpperCase();
  1368.       var uName = userName.toUpperCase();
  1369.  
  1370.       // confirm these are real names
  1371.       var tEntity = new Query();
  1372.       tEntity.database = this._database;
  1373.       tEntity.sql = 'select * from smentity';
  1374.       tEntity.active = true;
  1375.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + gName + "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  1376.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  1377.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + uName + "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  1378.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  1379.  
  1380.       // check for current assignment
  1381.       var tAssign = new Query();
  1382.       tAssign.database = this._database;
  1383.       tAssign.sql = 'select * from smassign';
  1384.       tAssign.active = true;
  1385.       if (tAssign.rowset.applyLocate("Parent = '" + gName + "' and Child = '" + uName + "' and " + '"Assign Type" = ' + SM_ASSIGN_GROUP_USER)) {
  1386.          tAssign.rowset.delete();
  1387.       }
  1388.       return (true);
  1389.    }
  1390.  
  1391.    function unassignResourceGroup(resourceName, groupName) {
  1392.       // check for error conditions
  1393.       if (!this._admin)
  1394.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1395.       if (resourceName == null)
  1396.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  1397.       if (groupName == null)
  1398.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  1399.  
  1400.       // convert names to uppercase
  1401.       var rName = resourceName.toUpperCase();
  1402.       var gName = groupName.toUpperCase();
  1403.  
  1404.       // confirm these are real names
  1405.       var tEntity = new Query();
  1406.       tEntity.database = this._database;
  1407.       tEntity.sql = 'select * from smentity';
  1408.       tEntity.active = true;
  1409.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + rName + "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  1410.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);    
  1411.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + gName + "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  1412.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  1413.  
  1414.       // check for current assignment
  1415.       var tAssign = new Query();
  1416.       tAssign.database = this._database;
  1417.       tAssign.sql = 'select * from smassign';
  1418.       tAssign.active = true;
  1419.       if (tAssign.rowset.applyLocate("Parent = '" + rName + "' and Child = '" + gName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_GROUP)) {
  1420.          tAssign.rowset.delete();
  1421.       }
  1422.       return (true);
  1423.    }
  1424.  
  1425.    function unassignResourceUser(resourceName, userName) {
  1426.       // check for error conditions
  1427.       if (!this._admin)
  1428.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1429.       if (resourceName == null)
  1430.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  1431.       if (userName == null)
  1432.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  1433.  
  1434.       // convert names to uppercase
  1435.       var rName = resourceName.toUpperCase();
  1436.       var uName = userName.toUpperCase();
  1437.  
  1438.       // confirm these are real names
  1439.       var tEntity = new Query();
  1440.       tEntity.database = this._database;
  1441.       tEntity.sql = 'select * from smentity';
  1442.       tEntity.active = true;
  1443.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + rName + "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  1444.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  1445.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + uName + "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  1446.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  1447.  
  1448.       // check for current assignment
  1449.       var tAssign = new Query();
  1450.       tAssign.database = this._database;
  1451.       tAssign.sql = 'select * from smassign';
  1452.       tAssign.active = true;
  1453.       if (tAssign.rowset.applyLocate("Parent = '" + rName + "' and Child = '" + uName + "' and " + '"Assign Type" = ' + SM_ASSIGN_RESOURCE_USER)) {
  1454.          tAssign.rowset.delete();
  1455.       }
  1456.       return (true);
  1457.    }
  1458.  
  1459.    function updateGroup(group) {
  1460.       // check for error conditions
  1461.       if (!this._admin)
  1462.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1463.       if (group.name == null)
  1464.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  1465.  
  1466.       // convert group name to uppercase
  1467.       gName = group.name.toUpperCase();
  1468.  
  1469.       var tEntity = new Query();
  1470.       tEntity.database = this._database;
  1471.       tEntity.sql = 'select * from smentity';
  1472.       tEntity.active = true;
  1473.  
  1474.       // throw exception on missing record
  1475.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + gName + "'" + ' and "Entity Type" = ' + SM_ENTITY_GROUP))
  1476.          throw new SmException(SM_ERROR_INVALID_GROUPNAME);
  1477.  
  1478.       // update this group
  1479.       tEntity.rowset.fields["Description"].value = group.description;
  1480.       tEntity.rowset.save();
  1481.  
  1482.       return (true);
  1483.    }
  1484.  
  1485.    function updatePolicy(policy) {
  1486.       // check for error conditions
  1487.       if (!this._admin)
  1488.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1489.       if (policy.name == null)
  1490.          throw new SmException(SM_ERROR_INVALID_POLICYNAME);
  1491.  
  1492.       // convert policy name to uppercase
  1493.       pName = policy.name.toUpperCase();
  1494.  
  1495.       var tEntity = new Query();
  1496.       tEntity.database = this._database;
  1497.       tEntity.sql = 'select * from smentity';
  1498.       tEntity.active = true;
  1499.  
  1500.       // throw exception on missing record
  1501.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + pName + "'" + ' and "Entity Type" = ' + SM_ENTITY_POLICY))
  1502.          throw new SmException(SM_ERROR_INVALID_POLICYNAME);
  1503.  
  1504.       // See if changing type of locked policy
  1505.       if (tEntity.rowset.fields["Entity Lock"].value && (policy.boolean != tEntity.rowset.fields["Policy Boolean"].value))
  1506.          throw new SmException(SM_ERROR_CAN_NOT_CHANGE_SYSTEM_ENTITY_TYPE);
  1507.  
  1508.       // update this policy
  1509.       tEntity.rowset.fields["Description"].value = policy.description;
  1510.       if (policy.boolean != null)
  1511.          tEntity.rowset.fields["Policy Boolean"].value     = policy.boolean;
  1512.       if (policy.value != null)
  1513.          tEntity.rowset.fields["Policy Value"].value       = 
  1514.             (policy.boolean ? (policy.value ? 1 : 0 ) : policy.value);
  1515.       tEntity.rowset.save();
  1516.  
  1517.       return (true);
  1518.    }
  1519.  
  1520.    function updateResource(resource) {
  1521.       // check for error conditions
  1522.       if (!this._admin)
  1523.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1524.       if (resource.name == null)
  1525.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  1526.  
  1527.       // convert resource name to uppercase
  1528.       rName = resource.name.toUpperCase();
  1529.  
  1530.       var tEntity = new Query();
  1531.       tEntity.database = this._database;
  1532.       tEntity.sql = 'select * from smentity'
  1533.       tEntity.active = true;
  1534.  
  1535.       // throw exception on missing record
  1536.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + rName + "'" + ' and "Entity Type" = ' + SM_ENTITY_RESOURCE))
  1537.          throw new SmException(SM_ERROR_INVALID_RESOURCENAME);
  1538.  
  1539.       // update this resource
  1540.       tEntity.rowset.fields["Description"].value = resource.description;
  1541.       tEntity.rowset.save();
  1542.  
  1543.       return (true);
  1544.    }
  1545.  
  1546.    function updateUser(user) {
  1547.       // check for error conditions
  1548.       if (!this._admin)
  1549.          throw new SmException(SM_ERROR_INSUFFICIENT_RIGHTS);
  1550.       if (user.name == null)
  1551.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  1552.  
  1553.       // convert user name to uppercase
  1554.       uName = user.name.toUpperCase();
  1555.  
  1556.       var tEntity = new Query();
  1557.       tEntity.database = this._database;
  1558.       tEntity.sql = 'select * from smentity';
  1559.       tEntity.active = true;
  1560.  
  1561.       // throw exception on missing record
  1562.       if (!tEntity.rowset.applyLocate('"Entity Name" = ' + "'" + uName + "'" + ' and "Entity Type" = ' + SM_ENTITY_USER))
  1563.          throw new SmException(SM_ERROR_INVALID_USERNAME);
  1564.  
  1565.       // update this user
  1566.       tEntity.rowset.fields["Description"].value   = user.description;
  1567.       tEntity.rowset.fields["User Disabled"].value = user.disabled;
  1568.       tEntity.rowset.fields["User Lockout"].value  = user.lockout;
  1569.       if (user.password != null)
  1570.          tEntity.rowset.fields["User Password"].value  = user.password;
  1571.       tEntity.rowset.save();
  1572.  
  1573.       return (true);
  1574.    }
  1575.  
  1576. }
  1577.  
  1578. class SmGroup {
  1579.    this.created     = null;
  1580.    this.description = null;
  1581.    this.name        = null;
  1582. }
  1583.  
  1584. class SmPolicy {
  1585.    this.boolean     = null;
  1586.    this.created     = null;
  1587.    this.description = null;
  1588.    this.name        = null;
  1589.    this.value       = null;
  1590. }
  1591.  
  1592. class SmResource {
  1593.    this.created     = null;
  1594.    this.description = null;
  1595.    this.name        = null;
  1596. }
  1597.  
  1598. class SmUser {
  1599.    this.created     = null;
  1600.    this.description = null;
  1601.    this.disabled    = null;
  1602.    this.login       = null;
  1603.    this.lockout     = null;
  1604.    this.name        = null;
  1605.    this.password    = null;
  1606. }
  1607.  
  1608. class SmException(code) extends Exception {
  1609.    this.code = code;
  1610.    this.message = "Security Manager Error";
  1611. }
  1612.  
  1613.  
  1614.