1 /*
  2  * Copyright © [2008-2009] Novell, Inc.  All Rights Reserved.
  3  * 
  4  * USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE DEVELOPER LICENSE AGREEMENT
  5  * OR OTHER AGREEMENT THROUGH WHICH NOVELL, INC. MAKES THE WORK AVAILABLE.  THIS WORK 
  6  * MAY NOT BE ADAPTED WITHOUT NOVELL'S PRIOR WRITTEN CONSENT.
  7  * 
  8  * NOVELL PROVIDES THE WORK "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, 
  9  * INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR 
 10  * A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  NOVELL, THE AUTHORS OF THE WORK, AND THE 
 11  * OWNERS OF COPYRIGHT IN THE WORK ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER 
 12  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, 
 13  * OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN THE WORK.
 14  */
 15 
 16 /**
 17  * @fileoverview
 18  * This file defines the Tenant class, which represents source tenants or domains from
 19  * which input data is received (corresponding to the TenantName field in event data
 20  * and tenant flags on all other data).
 21  * @name Tenant Class
 22  */
 23 
 24 /**
 25  * Creates an instance of the Tenant class.
 26  * @class
 27  * The Tenant class represents the Tenant information that is assigned to data stored in Sentinel.
 28  * All data (events, identities, assets, vulnerabilities) are flagged as belonging to one or more
 29  * tenants. This is intended for use in multi-tenant environments where data
 30  * from each tenant should be kept separate, but can also be used by single enterprises to keep 
 31  * different sections of the enterprise's data apart.
 32  * Each tenant is identified by a name and an internal ID. In most cases you only need to know
 33  * the name, which is usually set as the <code>instance.CONFIG.params.TenantName</code> parameter.
 34  * Ordinarily you will just place this name in events sent to the ESM framework. In some cases, however,
 35  * other data objects want the Tenant ID instead of the name; use this class for this situation.
 36  * The class accepts these pre-defined attributes:
 37  * <ul>
 38  * <li>Name {String} : The name of the tenant 
 39  * <li>ID {String} : The ID of the tenant
 40  * </ul>
 41  * Note that if the tenant has not yet been seen by the system, there will be no corresponding record
 42  * in the database and the getId() method will fail. This will only happen the first time, however; after
 43  * a single event or data object referencing the tenant has been sent, the Tenant object will be
 44  * created and stored.
 45  * @example
 46  * instance.CONFIG.tenant = new Tenant({"Name":instance.CONFIG.params.TenantName});
 47  * var asset = new Asset({IPv4:"10.0.0.24",Vendor:"Novell",Tenant:instance.CONFIG.tenant});
 48  * @author   Novell Engineering
 49  * @version  6.1
 50  * @param {Object} properties  Set of pre-defined properties used to initialize this object
 51  * @constructor
 52  */
 53 function Tenant(properties) {
 54 	this.Name = "unknown";
 55 	if( typeof properties != "undefined") {
 56 		// Poor man's Impl
 57 		for (var field in properties) {
 58 			if (typeof TenantImpl[field] != "undefined") {
 59 				this[field] = properties[field];
 60 			}
 61 		}
 62 	}
 63 	
 64 	if (this.Name) {
 65 		this.ID = Tenant.getId(this.Name);
 66 	}
 67 	
 68 	return true;
 69 	
 70 	/**
 71 	 * 
 72 	 * @type String
 73 	 */
 74 	this.Name;
 75 	
 76 	/**
 77 	 * 
 78 	 * @type String
 79 	 */
 80 	this.ID;
 81 
 82 }
 83 
 84 // Used as a reference for what attributes are allowed
 85 TenantImpl = {"Name":null,"ID":null};
 86 
 87 
 88 /**
 89 * The getId method returns an ID for a given tenant name.
 90 * <p>Example:
 91 * <pre>
 92 * var id = Tenant.getId("Novell")
 93 * </pre>
 94 * @param {String} The tenant name, usually stored in <code>instance.CONFIG.params.TenantName</code>
 95 * @return {ID}	a numeric value associated with the name
 96 * @throws {Exception} Throws an exception if there is a database error
 97 */
 98 Tenant.getId = function() {
 99 	var serviceName = Packages.esecurity.db.object.MsspService.SERVICE_NAME;
100 	var serviceManager = Packages.esecurity.base.datamodel.service.ServiceManager.getInstance();
101 	var service = serviceManager.getService(serviceName);
102 	
103 	return function(custName) {
104 		var cust = service.getCustomer(custName,true);
105 		return cust.getStringCustomerId();
106 	};
107 }();
108 
109 
110 
111