diff --git a/python/mocha-shell.py b/python/mocha-shell.py index 46c460d..1615846 100644 --- a/python/mocha-shell.py +++ b/python/mocha-shell.py @@ -191,6 +191,8 @@ class MochaShell (REPLApplication): return prompt def process_input(self, value): + print(value) + parms = value.split(' ') if len(parms) > 0: @@ -264,7 +266,11 @@ class MochaShell (REPLApplication): self.print_error_not_open() return - instances = self.oms.get_instances() + ik_of = None + if len(parms) > 2: + ik_of = self.parse(parms[2]) + + instances = self.oms.get_instances(ik_of) self.print_instance_list(instances) elif parms[0] == "attribute": @@ -354,6 +360,45 @@ class MochaShell (REPLApplication): print ("usage: relationship list instance_id") + elif parms[0] == "user": + + if parms[1] == "list": + + ik_User = self.oms.get_instance_by_global_identifier(KnownClassGuids.User) + ik_UserName = self.oms.get_instance_by_global_identifier(KnownAttributeGuids.UserName) + + print ("user name ") + print ("---------------------") + users = self.oms.get_instances(ik_User) + for user in users: + user_name = self.oms.get_attribute_value(user, ik_UserName) + print(user_name) + + return + + elif parms[1] == "set-password": + + if len(parms) > 2: + + my_username = parms[2] + my_user = self.oms.get_user_by_username(my_username) + + if my_user is None: + print("passwd: user '" + my_username + "' does not exist") + return + + print("Changing password for " + my_username + ".") + pw = getpass("New password: ") + pw2 = getpass("Confirm new password: ") + if pw == pw2: + + self.oms.set_user_password(my_user, pw) + print("passwd: password updated successfully") + + else: + print("Sorry, passwords do not match.") + return + elif parms[0] == "close": self.oms.close() diff --git a/python/mocha/definitions/KnownInstanceGuids.py b/python/mocha/definitions/KnownInstanceGuids.py index 16f4faa..a1c647e 100644 --- a/python/mocha/definitions/KnownInstanceGuids.py +++ b/python/mocha/definitions/KnownInstanceGuids.py @@ -3,5 +3,12 @@ from framework import Guid class User: superuser = Guid.parse("{69e291d8-381b-4ad8-9013-f3b0a0c693fe}") + + zq_environments = Guid.parse("{B066A54B-B160-4510-A805-436D3F90C2E6}") + zq_developer = Guid.parse("{098DDA82-CD04-4B53-8C75-89D420EA6902}") + zq_support = Guid.parse("{232A8CBF-0D2B-4BDA-BE86-3E2FA25A3FB5}") + zq_configurator = Guid.parse("{FB20A79C-EAA2-4A98-A1DA-BDC351854694}") + zq_implementer = Guid.parse("{63F2EF51-DC73-48EC-856A-6FBBEDE01A8A}") + admin = Guid.parse("{739C26BC-740F-4CB0-BCB1-2A28FA570E7D}") \ No newline at end of file diff --git a/python/mocha/oms/Oms.py b/python/mocha/oms/Oms.py index 78eed9e..f5df231 100644 --- a/python/mocha/oms/Oms.py +++ b/python/mocha/oms/Oms.py @@ -62,9 +62,48 @@ class Oms: def get_tenant_by_name(self, tenant_name : str) -> TenantReference: pass - def create_instance_of(self, class_inst : InstanceReference, global_identifier : Guid = None): + def _create_instance_of(self, class_inst : InstanceReference, global_identifier : Guid = None): pass + def create_instance_of(self, class_inst : InstanceReference, global_identifier : Guid = None): + + inst = self._create_instance_of(class_inst, global_identifier) + + rel_Class__has__Instance = self.get_instance_by_global_identifier(KnownRelationshipGuids.Class__has__Instance) + if rel_Class__has__Instance is None: + return inst + rel_Instance__for__Class = self.get_instance_by_global_identifier(KnownRelationshipGuids.Instance__for__Class) + if rel_Instance__for__Class is None: + return inst + + self.assign_relationship(class_inst, rel_Class__has__Instance, inst) + self.assign_relationship(inst, rel_Instance__for__Class, class_inst) + + return inst + + def set_user_password(self, user : InstanceReference, password : str): + + ik_UserName = self.get_instance_by_global_identifier(KnownAttributeGuids.UserName) + ik_PasswordHash = self.get_instance_by_global_identifier(KnownAttributeGuids.PasswordHash) + ik_PasswordSalt = self.get_instance_by_global_identifier(KnownAttributeGuids.PasswordSalt) + + username = self.get_attribute_value(user, self.get_instance_by_global_identifier(KnownAttributeGuids.UserName)) + salt = Guid.create().strip() + + passsalt = str(password + salt).encode("utf-8") + import hashlib + passhash = hashlib.sha512(passsalt).hexdigest() + + self.assign_attribute(user, ik_UserName, username) + self.assign_attribute(user, ik_PasswordHash, passhash) + self.assign_attribute(user, ik_PasswordSalt, salt) + + def create_user(self, username : str, password : str, global_identifier : Guid = None): + user = self.create_instance_of(self.get_instance_by_global_identifier(KnownClassGuids.User), global_identifier) + + if password is not None: + self.assign_user_password(user, password) + def before_init(self): pass @@ -89,6 +128,8 @@ class Oms: ik_Singular = self.create_instance_of(self.get_instance_by_global_identifier(KnownClassGuids.BooleanAttribute), KnownAttributeGuids.Singular) ik_RelationshipType = self.create_instance_of(self.get_instance_by_global_identifier(KnownClassGuids.TextAttribute), KnownAttributeGuids.RelationshipType) + + ik_Instance = self.create_class(1, 17, "Instance", KnownClassGuids.Instance) ik_User = self.create_class(1, 39, 'System User', KnownClassGuids.User) ik_UserLogin = self.create_class(1, 4328, 'System Account Signon', KnownClassGuids.UserLogin) @@ -106,6 +147,11 @@ class Oms: self.assign_attribute(superuser, ik_PasswordHash, "0bdbeeecd24e93a0d84ff2946c519761d4db2c52d9449d2de5bf3b74a8bcdfbee9d72bb88121a63700d09554983e7f881021ecf493f83dec19e8f7e465529c34") self.assign_attribute(superuser, ik_PasswordSalt, "5e28b86839ed4ca9838c112723aeaa27") + self.create_user("zq-configurator", "Florida407!", KnownInstanceGuids.User.zq_configurator) + self.create_user("zq-developer", "Florida407!", KnownInstanceGuids.User.zq_developer) + self.create_user("zq-environments", "Florida407!", KnownInstanceGuids.User.zq_environments) + self.create_user("zq-support", "Florida407!", KnownInstanceGuids.User.zq_support) + self.assign_attribute(self.get_instance_by_key(InstanceKey(1, 2)), self.get_instance_by_key(InstanceKey(4, 1)), 'Attribute') self.assign_attribute(self.get_instance_by_key(InstanceKey(1, 3)), self.get_instance_by_key(InstanceKey(4, 1)), 'Relationship') self.assign_attribute(self.get_instance_by_key(InstanceKey(1, 4)), self.get_instance_by_key(InstanceKey(4, 1)), 'Text Attribute') @@ -119,15 +165,37 @@ class Oms: self.update_relationship(rel_Relationship__has_source__Class, ik_Relationship, "has source", ik_Class, True, None) self.update_relationship(rel_Relationship__has_destination__Class, ik_Relationship, "has destination", ik_Class, True, None) self.update_relationship(rel_Relationship__has_sibling__Relationship, ik_Relationship, "has sibling", ik_Relationship, True, None) + + rel_Class__has__Instance = self.create_relationship(ik_Class, "has", ik_Instance, False, None, KnownRelationshipGuids.Class__has__Instance) + rel_Instance__for__Class = self.create_relationship(ik_Instance, "for", ik_Class, True, None, KnownRelationshipGuids.Instance__for__Class) + self.update_relationship(rel_Class__has__Instance, None, None, None, None, rel_Instance__for__Class) + self.update_relationship(rel_Instance__for__Class, None, None, None, None, rel_Class__has__Instance) + + self.update_class_instances() def update_relationship(self, relationship : InstanceReference, src_class : InstanceReference, type : str, dst_class : InstanceReference, singular: bool, sibling_relationship : InstanceReference): - self.assign_relationship(relationship, self.get_instance_by_global_identifier(KnownRelationshipGuids.Relationship__has_source__Class), src_class) - self.assign_attribute(relationship, self.get_instance_by_global_identifier(KnownAttributeGuids.RelationshipType), type) - self.assign_relationship(relationship, self.get_instance_by_global_identifier(KnownRelationshipGuids.Relationship__has_destination__Class), dst_class) - self.assign_attribute(relationship, self.get_instance_by_global_identifier(KnownAttributeGuids.Singular), singular) - + if src_class is not None: + self.assign_relationship(relationship, self.get_instance_by_global_identifier(KnownRelationshipGuids.Relationship__has_source__Class), src_class) + if type is not None: + self.assign_attribute(relationship, self.get_instance_by_global_identifier(KnownAttributeGuids.RelationshipType), type) + if dst_class is not None: + self.assign_relationship(relationship, self.get_instance_by_global_identifier(KnownRelationshipGuids.Relationship__has_destination__Class), dst_class) + if singular is not None: + self.assign_attribute(relationship, self.get_instance_by_global_identifier(KnownAttributeGuids.Singular), singular) if sibling_relationship is not None: self.assign_relationship(relationship, self.get_instance_by_global_identifier(KnownRelationshipGuids.Relationship__has_sibling__Relationship), sibling_relationship) + + def update_class_instances(self): + + ik_Class = self.get_instance_by_global_identifier(KnownClassGuids.Class) + rel_Class__has__Instance = self.get_instance_by_global_identifier(KnownRelationshipGuids.Class__has__Instance) + rel_Instance__for__Class = self.get_instance_by_global_identifier(KnownRelationshipGuids.Instance__for__Class) + + insts = self.get_instances() + for inst in insts: + clasz = self.get_instance_by_key(InstanceKey(1, inst.get_instance_key().get_class_index() )) + self.assign_relationship(clasz, rel_Class__has__Instance, inst) + self.assign_relationship(inst, rel_Instance__for__Class, clasz) def after_init(self): pass diff --git a/python/mocha/oms/db/sqlite/SQLiteDatabaseOms.py b/python/mocha/oms/db/sqlite/SQLiteDatabaseOms.py index f8ce6af..33dfd3d 100644 --- a/python/mocha/oms/db/sqlite/SQLiteDatabaseOms.py +++ b/python/mocha/oms/db/sqlite/SQLiteDatabaseOms.py @@ -33,8 +33,8 @@ class SQLiteDatabaseOms (DatabaseOms): return result - def create_instance_of(self, class_inst : InstanceReference, global_identifier : Guid = None): - + def _create_instance_of(self, class_inst : InstanceReference, global_identifier : Guid = None): + if global_identifier is None: global_identifier = Guid.create()