public static object EncodeACL(LCACL acl) { HashSet <string> keys = new HashSet <string>(); if (acl.ReadAccess.Count > 0) { keys.UnionWith(acl.ReadAccess.Keys); } if (acl.WriteAccess.Count > 0) { keys.UnionWith(acl.WriteAccess.Keys); } Dictionary <string, object> result = new Dictionary <string, object>(); foreach (string key in keys) { Dictionary <string, bool> access = new Dictionary <string, bool>(); if (acl.ReadAccess.TryGetValue(key, out bool ra)) { access["read"] = ra; } if (acl.WriteAccess.TryGetValue(key, out bool wa)) { access["write"] = wa; } result[key] = access; } return(result); }
public async Task UserReadAndWrite() { await LCUser.Login("hello", "world"); LCObject account = new LCObject("Account"); LCUser currentUser = await LCUser.GetCurrent(); LCACL acl = LCACL.CreateWithOwner(currentUser); account.ACL = acl; account["balance"] = 512; await account.Save(); Assert.IsTrue(acl.GetUserReadAccess(currentUser)); Assert.IsTrue(acl.GetUserWriteAccess(currentUser)); LCQuery <LCObject> query = new LCQuery <LCObject>("Account"); LCObject result = await query.Get(account.ObjectId); TestContext.WriteLine(result.ObjectId); Assert.NotNull(result.ObjectId); await LCUser.Logout(); result = await query.Get(account.ObjectId); Assert.IsNull(result); }
public async Task RoleReadAndWrite() { LCUser currentUser = await LCUser.Login(TestPhone, TestPhone); string name = $"role_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"; LCACL roleACL = new LCACL(); roleACL.SetUserReadAccess(currentUser, true); roleACL.SetUserWriteAccess(currentUser, true); LCRole role = LCRole.Create(name, roleACL); role.AddRelation("users", currentUser); await role.Save(); account = new Account(); LCACL acl = new LCACL(); acl.SetRoleReadAccess(role, true); acl.SetRoleWriteAccess(role, true); account.ACL = acl; await account.Save(); Assert.IsTrue(acl.GetRoleReadAccess(role)); Assert.IsTrue(acl.GetRoleWriteAccess(role)); }
public async Task UserReadAndWrite() { await LCUser.Login(TestPhone, TestPhone); account = new Account(); LCUser currentUser = await LCUser.GetCurrent(); account.ACL = LCACL.CreateWithOwner(currentUser); account.Balance = 512; await account.Save(); Assert.IsTrue(account.ACL.GetUserReadAccess(currentUser)); Assert.IsTrue(account.ACL.GetUserWriteAccess(currentUser)); LCQuery <LCObject> query = new LCQuery <LCObject>("Account"); LCObject result = await query.Get(account.ObjectId); TestContext.WriteLine(result.ObjectId); Assert.NotNull(result.ObjectId); await LCUser.Logout(); try { await query.Get(account.ObjectId); } catch (LCException e) { Assert.AreEqual(e.Code, 403); } }
/// <summary> /// Constructs a LCRole with a name and a LCACL. /// </summary> /// <param name="name"></param> /// <param name="acl"></param> /// <returns></returns> public static LCRole Create(string name, LCACL acl) { LCRole role = new LCRole() { Name = name, ACL = acl }; return(role); }
public async Task PrivateReadAndWrite() { LCObject account = new LCObject("Account"); LCACL acl = new LCACL(); acl.PublicReadAccess = false; acl.PublicWriteAccess = false; account.ACL = acl; account["balance"] = 1024; await account.Save(); Assert.IsFalse(acl.PublicReadAccess); Assert.IsFalse(acl.PublicWriteAccess); }
public async Task NewRole() { LCUser currentUser = await LCUser.Login("game", "play"); LCACL acl = new LCACL(); acl.PublicReadAccess = true; acl.SetUserWriteAccess(currentUser, true); string name = $"role_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"; LCRole role = LCRole.Create(name, acl); role.AddRelation("users", currentUser); await role.Save(); }
public async Task RoleReadAndWrite() { LCQuery <LCRole> query = LCRole.GetQuery(); LCRole owner = await query.Get("5e1440cbfc36ed006add1b8d"); LCObject account = new LCObject("Account"); LCACL acl = new LCACL(); acl.SetRoleReadAccess(owner, true); acl.SetRoleWriteAccess(owner, true); account.ACL = acl; await account.Save(); Assert.IsTrue(acl.GetRoleReadAccess(owner)); Assert.IsTrue(acl.GetRoleWriteAccess(owner)); }
static object EncodeACL(LCACL acl) { HashSet <string> readers = acl.readers; HashSet <string> writers = acl.writers; HashSet <string> union = new HashSet <string>(readers); union.UnionWith(writers); Dictionary <string, object> dict = new Dictionary <string, object>(); foreach (string k in union) { dict[k] = new Dictionary <string, object> { { "read", readers.Contains(k) }, { "write", writers.Contains(k) } }; } return(dict); }
public static LCACL DecodeACL(Dictionary <string, object> dict) { LCACL acl = new LCACL(); foreach (KeyValuePair <string, object> kv in dict) { string key = kv.Key; Dictionary <string, object> access = kv.Value as Dictionary <string, object>; if (access.TryGetValue("read", out object ra)) { acl.ReadAccess[key] = Convert.ToBoolean(ra); } if (access.TryGetValue("write", out object wa)) { acl.WriteAccess[key] = Convert.ToBoolean(wa); } } return(acl); }
public async Task FileACL() { LCUser user = await LCUser.LoginAnonymously(); LCFile file = new LCFile("avatar", AvatarFilePath); LCACL acl = new LCACL(); acl.SetUserReadAccess(user, true); file.ACL = acl; await file.Save(); LCQuery <LCFile> query = LCFile.GetQuery(); LCFile avatar = await query.Get(file.ObjectId); Assert.NotNull(avatar.ObjectId); await LCUser.LoginAnonymously(); try { LCFile forbiddenAvatar = await query.Get(file.ObjectId); } catch (LCException e) { Assert.AreEqual(e.Code, 403); } }