From 8763ac805e0f287322dadb380575a828e3ac51ab Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Sun, 7 Jul 2019 02:49:34 +0300 Subject: [PATCH] Bugfix --- Esiur.Stores.MongoDB/MongoDBStore.cs | 42 ++++++++++++++----- Esiur/Resource/Resource.cs | 12 +++++- .../Permissions/UserPermissionsManager.cs | 3 +- Test/MyMembership.cs | 14 ++++++- 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Esiur.Stores.MongoDB/MongoDBStore.cs b/Esiur.Stores.MongoDB/MongoDBStore.cs index fd02924..932cbf1 100644 --- a/Esiur.Stores.MongoDB/MongoDBStore.cs +++ b/Esiur.Stores.MongoDB/MongoDBStore.cs @@ -22,6 +22,8 @@ namespace Esiur.Stores.MongoDB MongoClient client; IMongoDatabase database; IMongoCollection resourcesCollection; + string collectionName; + string dbName; Dictionary resources = new Dictionary(); @@ -49,7 +51,7 @@ namespace Esiur.Stores.MongoDB {"property", propertyName}, {"age", BsonValue.Create(age) }, {"date", date}, {"value", Compose(value) } }); - var col = this.database.GetCollection("resources"); + var col = this.database.GetCollection(collectionName); @@ -64,19 +66,19 @@ namespace Esiur.Stores.MongoDB return true; } - public MongoDBStore() + public MongoDBStore() : this("mongodb://localhost", "esiur", "resources") { - client = new MongoClient(); - this.database = client.GetDatabase("esiur"); - this.resourcesCollection = this.database.GetCollection("resources"); } - public MongoDBStore(string connectionString, string database) + public MongoDBStore(string connectionString, string database, string collection) { + collectionName = collection; + dbName = database; + client = new MongoClient(connectionString); this.database = client.GetDatabase(database); - this.resourcesCollection = this.database.GetCollection("resources"); + this.resourcesCollection = this.database.GetCollection(collection); } public bool Remove(IResource resource) @@ -274,6 +276,20 @@ namespace Esiur.Stores.MongoDB return true; } + // insert the document + var document = new BsonDocument + { + { "classname", resource.GetType().FullName + "," + resource.GetType().GetTypeInfo().Assembly.GetName().Name }, + { "name", resource.Instance.Name }, + }; + + resourcesCollection.InsertOne(document); + resource.Instance.Attributes["objectId"] = document["_id"].ToString(); + + + // now update the document + // * insert first to get the object id, update values, attributes, children and parents after in case the same resource has a property references self + var parents = new BsonArray(); var children = new BsonArray(); @@ -314,6 +330,7 @@ namespace Esiur.Stores.MongoDB // col.UpdateOne(filter, update); + /* var document = new BsonDocument { { "parents", parents }, @@ -323,11 +340,15 @@ namespace Esiur.Stores.MongoDB { "name", resource.Instance.Name }, { "values", values } }; + */ + + var filter = Builders.Filter.Eq("_id", document["_id"]); + var update = Builders.Update + .Set("values", values).Set("parents", parents).Set("children", children).Set("attributes", attrsDoc); + resourcesCollection.UpdateOne(filter, update); - resourcesCollection.InsertOne(document); - - resource.Instance.Attributes["objectId"] = document["_id"].ToString(); + //resource.Instance.Attributes["objectId"] = document["_id"].ToString(); return true; } @@ -398,6 +419,7 @@ namespace Esiur.Stores.MongoDB case DataType.Resource: case DataType.DistributedResource: + return new BsonDocument { { "type", 0 }, { "link", (value as IResource).Instance.Link } }; //return new BsonObjectId(new ObjectId((string)(value as IResource).Instance.Attributes["objectId"])); diff --git a/Esiur/Resource/Resource.cs b/Esiur/Resource/Resource.cs index 336ceda..5b5474e 100644 --- a/Esiur/Resource/Resource.cs +++ b/Esiur/Resource/Resource.cs @@ -36,12 +36,20 @@ namespace Esiur.Resource public virtual void Destroy() { - + } public virtual AsyncReply Trigger(ResourceTrigger trigger) { - return new AsyncReply(true); + if (trigger == ResourceTrigger.Initialize) + return new AsyncReply(this.Create()); + else + return new AsyncReply(true); + } + + public virtual bool Create() + { + return true; } } } diff --git a/Esiur/Security/Permissions/UserPermissionsManager.cs b/Esiur/Security/Permissions/UserPermissionsManager.cs index 1a593dd..c2f58d5 100644 --- a/Esiur/Security/Permissions/UserPermissionsManager.cs +++ b/Esiur/Security/Permissions/UserPermissionsManager.cs @@ -96,8 +96,7 @@ namespace Esiur.Security.Permissions if ((string)userPermissions["_rename"] != "yes") return Ruling.Denied; } - - if (userPermissions.ContainsKey(member.Name)) + else if (userPermissions.ContainsKey(member?.Name)) { Structure methodPermissions = userPermissions[member.Name] as Structure; if ((string)methodPermissions[action.ToString()] != "yes") diff --git a/Test/MyMembership.cs b/Test/MyMembership.cs index 249041e..5034f95 100644 --- a/Test/MyMembership.cs +++ b/Test/MyMembership.cs @@ -1,6 +1,7 @@ using Esiur.Data; using Esiur.Engine; using Esiur.Resource; +using Esiur.Security.Authority; using Esiur.Security.Membership; using System; using System.Collections.Generic; @@ -25,12 +26,23 @@ namespace Test return new AsyncReply(DC.ToBytes("1234")); } + public AsyncReply Login(Session session) + { + return new AsyncReply(true); + } + + public AsyncReply Logout(Session session) + { + return new AsyncReply(true); + } + public AsyncReply Trigger(ResourceTrigger trigger) { return new AsyncReply(true); } - public AsyncReply UserExists(string username) + + public AsyncReply UserExists(string username, string domain) { return new AsyncReply(username == "demo"); }