Example #1
0
        public static async Task <RolePriority> GetMaxPriority(User u)
        {
            UserServiceProxy usrv = new UserServiceProxy();
            var l = await usrv.MaterializeAllUsersInRole_UserIDsAsync(Cntx, u);

            if (l == null || l.Count() == 0)
            {
                return new RolePriority {
                           Major = -1, Minor = -1
                }
            }
            ;
            UsersInRoleServiceProxy uisvc = new UsersInRoleServiceProxy();

            foreach (var ir in l)
            {
                ir.RoleRef = uisvc.MaterializeRoleRef(Cntx, ir);
            }
            var uir     = (from d in l orderby d.RoleRef.RolePriority descending, d.SubPriority descending select d).First();
            var roleids = (from d in l orderby d.RoleRef.RolePriority select d.RoleID).ToArray();

            return(new RolePriority {
                Major = uir.RoleRef.RolePriority, Minor = uir.SubPriority, RoleIds = roleids, MaxRole = uir.RoleRef
            });
        }
Example #2
0
        public static async Task <OperationResult> AdjustUserRoleLevel(string adminId, string uid, int rid, int del)
        {
            OperationResult OpResult = new OperationResult();
            var             maxp     = await MemberAdminContext.GetMaxPriority(adminId);

            var cntx = Cntx;
            UserServiceProxy usvc = new UserServiceProxy();
            var u = usvc.LoadEntityByKey(cntx, uid);

            if (u == null)
            {
                OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
                return(OpResult);
            }
            UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
            var uir = await uirsvc.LoadEntityByKeyAsync(cntx, rid, u.ID);

            if (uir == null)
            {
                OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("78257cace857db766d54e6568d7f912b", "The user is not in this role.") };
                return(OpResult);
            }
            uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(cntx, uir);

            if (maxp.Major < uir.RoleRef.RolePriority || maxp.Major == uir.RoleRef.RolePriority && uir.SubPriority + del > maxp.Major)
            {
                OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("5986d63fe301793ee7f5b2134a8f8787", "Modifying more priviledged role is not authorized.") };
                return(OpResult);
            }
            var oldPrio = uir.SubPriority;

            uir.SubPriority += del;
            uir.LastModified = DateTime.UtcNow;
            uir.AdminID      = adminId;
            await uirsvc.AddOrUpdateEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });

            uir.UserID = u.ID;
            uir.RoleID = rid;
            await AddUserRoleHistory(uir, UserRoleOperations.Modified);

            UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
            var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);

            var notice = new SimpleMessage
            {
                TypeId = 1,
                Title  = string.Format(ResourceUtils.GetString("54da39696e8014b5ded7a0eaeac1dfc4", "The relative priority of your role: [{0}] is changed from {1} to {2}.", memb.AcceptLanguages),
                                       uir.RoleRef.DistinctString, oldPrio, uir.SubPriority),
                Data = "{ id=\"" + rid + "\", type=\"role\", name=\"" + uir.RoleRef.DistinctString + "\" }"
            };

            OpResult.Result  = new { ok = true, msg = "" };
            OpResult.notices = new SimpleMessage[] { notice };
            return(OpResult);
        }
 public static async Task<RolePriority> GetMaxPriority(User u)
 {
     UserServiceProxy usrv = new UserServiceProxy();
     var l = await usrv.MaterializeAllUsersInRole_UserIDsAsync(Cntx, u);
     if (l == null || l.Count() == 0)
         return new RolePriority { Major = -1, Minor = -1 };
     UsersInRoleServiceProxy uisvc = new UsersInRoleServiceProxy();
     foreach (var ir in l)
         ir.RoleRef = uisvc.MaterializeRoleRef(Cntx, ir);
     var uir = (from d in l orderby d.RoleRef.RolePriority descending, d.SubPriority descending select d).First();
     var roleids = (from d in l orderby d.RoleRef.RolePriority select d.RoleID).ToArray();
     return new RolePriority { Major = uir.RoleRef.RolePriority, Minor = uir.SubPriority, RoleIds = roleids, MaxRole = uir.RoleRef };
 }
Example #4
0
        public static async Task <OperationResult> RemoveUserFromRole(string adminId, string uid, int rid)
        {
            OperationResult OpResult = new OperationResult();
            var             maxp     = await MemberAdminContext.GetMaxPriority(adminId);

            var cntx = Cntx;
            UserServiceProxy usvc = new UserServiceProxy();
            var u = await usvc.LoadEntityByKeyAsync(cntx, uid);

            if (u == null)
            {
                OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
                return(OpResult);
            }
            UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
            var uir = await uirsvc.LoadEntityByKeyAsync(cntx, rid, u.ID);

            if (uir == null)
            {
                OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("78257cace857db766d54e6568d7f912b", "The user is not in this role.") };
                return(OpResult);
            }
            uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(cntx, uir);

            if (maxp.Major < uir.RoleRef.RolePriority || maxp.Major == uir.RoleRef.RolePriority && uir.SubPriority > maxp.Major)
            {
                OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("0437b5660f17723dc29c3fa7e08e08a0", "Removing more priviledged role is not authorized.") };
                return(OpResult);
            }
            await uirsvc.DeleteEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });

            uir.UserID = u.ID;
            uir.RoleID = rid;
            await AddUserRoleHistory(uir, UserRoleOperations.Deleted);

            UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
            var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);

            OpResult.Result  = new { ok = true, msg = "", available = new { id = rid, name = uir.RoleRef.RoleName, path = uir.RoleRef.DistinctString, op = true } };
            OpResult.notices = new SimpleMessage[]
            {
                new SimpleMessage
                {
                    TypeId = 1,
                    Title  = string.Format(ResourceUtils.GetString("9708d527fbbf0d9752fc2c741615fb58", "Your role: [{0}] is removed.", memb.AcceptLanguages), uir.RoleRef.DistinctString),
                    Data   = "{ id=\"" + rid + "\", type=\"role\", name=\"" + uir.RoleRef.DistinctString + "\" }"
                }
            };
            return(OpResult);
        }
 public static async Task<string> GetMembers(string set, string qexpr, string prevlast)
 {
     JavaScriptSerializer jser = new JavaScriptSerializer();
     dynamic sobj = jser.DeserializeObject(set) as dynamic;
     DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(QueryExpresion));
     DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(User));
     var ser3 = new JavaScriptSerializer();
     System.IO.MemoryStream strm = new System.IO.MemoryStream();
     byte[] sbf = System.Text.Encoding.UTF8.GetBytes(qexpr);
     strm.Write(sbf, 0, sbf.Length);
     strm.Position = 0;
     var _qexpr = ser1.ReadObject(strm) as QueryExpresion;
     UserServiceProxy svc = new UserServiceProxy();
     UserSet _set = new UserSet();
     _set.PageBlockSize = int.Parse(sobj["pageBlockSize"]);
     _set.PageSize_ = int.Parse(sobj["pageSize"]);
     if (sobj.ContainsKey("setFilter"))
         _set.SetFilter = sobj["setFilter"];
     User _prevlast = null;
     if (!string.IsNullOrEmpty(prevlast))
     {
         strm = new System.IO.MemoryStream();
         sbf = System.Text.Encoding.UTF8.GetBytes(prevlast);
         strm.Write(sbf, 0, sbf.Length);
         strm.Position = 0;
         _prevlast = ser2.ReadObject(strm) as User;
     }
     var result = await svc.GetPageItemsAsync(Cntx, _set, _qexpr, _prevlast);
     var ar = new List<dynamic>();
     string appId = ApplicationContext.App.ID;
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     foreach (var e in result)
     {
         //var membs = svc.MaterializeAllUserAppMembers(Cntx, e);
         //var memb = (from d in membs where d.ApplicationID == appId select d).SingleOrDefault();
         UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
         var cond = new UserAppMemberSetConstraints 
         { 
             ApplicationIDWrap = new ForeignKeyData<string> { KeyValue = appId }, 
             UserIDWrap = new ForeignKeyData<string> { KeyValue = e.ID } 
         };
         var memb = (await mbsvc.ConstraintQueryAsync(Cntx, new UserAppMemberSet(), cond, null)).SingleOrDefault();
         ar.Add(new { data = e, member = memb, hasIcon = memb != null && !string.IsNullOrEmpty(memb.IconMime) });
     }
     string json = ser3.Serialize(ar);
     return json;
 }
Example #6
0
        public static async Task <dynamic> ListUsersInRole(string adminId, int id)
        {
            RoleServiceProxy rsvc = new RoleServiceProxy();
            var r = await rsvc.LoadEntityByKeyAsync(Cntx, id);

            List <dynamic> users = new List <dynamic>();

            if (r == null)
            {
                return new { ok = false, msg = ResourceUtils.GetString("2dcb0c4ea3d378571beac6927e1a4a99", "The role is not found!"), users = users }
            }
            ;
            var maxp = await MemberAdminContext.GetMaxPriority(adminId);

            var uirs = await rsvc.MaterializeAllUsersInRolesAsync(Cntx, r);

            UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();

            foreach (var uir in uirs)
            {
                uir.User_UserID = await uirsvc.MaterializeUser_UserIDAsync(Cntx, uir);

                uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(Cntx, uir);

                var umax = await MemberAdminContext.GetMaxPriority(uir.User_UserID.ID);

                bool canOp = false;
                if (maxp.Major >= umax.Major)
                {
                    canOp = maxp.Major > r.RolePriority || maxp.Major == r.RolePriority && maxp.Minor >= uir.SubPriority;
                }
                users.Add(new { id = uir.RoleRef.ID, uid = uir.User_UserID.ID, name = uir.RoleRef.RoleName, username = uir.User_UserID.Username, path = Utils.GetHtmlRolePath(uir.RoleRef.DistinctString), level = uir.SubPriority, op = canOp });
            }
            return(new { ok = true, msg = "", users = users });
        }
    }
 public static async Task<OperationResult> AddUserToRole(string adminId, string uid, int rid)
 {
     OperationResult OpResult = new OperationResult();
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     RoleServiceProxy rsvc = new RoleServiceProxy();
     UserServiceProxy usvc = new UserServiceProxy();
     var cntx = Cntx;
     var u = await usvc.LoadEntityByKeyAsync(cntx, uid);
     if (u == null)
     {
         OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
         return OpResult;
     }
     var uroles = await usvc.MaterializeAllRolesAsync(cntx, u);
     if (DBAutoCleanupRoles)
     {
         // prevent polution
         List<Role> higherroles = new List<Role>();
         foreach (var ur in uroles)
         {
             var pr = ur;
             if (pr.ID == rid)
                 higherroles.Add(ur);
             while (pr.ParentID != null)
             {
                 pr.UpperRef = await rsvc.MaterializeUpperRefAsync(cntx, pr);
                 pr = pr.UpperRef;
                 if (pr.ID == rid)
                 {
                     higherroles.Add(ur);
                     break;
                 }
             }
         }
         if (higherroles.Count > 0)
         {
             string rolesstr = "";
             foreach (var hr in higherroles)
                 rolesstr += (rolesstr == "" ? "" : ", ") + hr.DistinctString;
             string errorfmt = ResourceUtils.GetString("43558b5deaec392b9461d28d4e753687", "Operation denied: the user already has this or more specific roles: '{0}'! Try to remove them before adding present one.");
             OpResult.Result = new { ok = false, msg = string.Format(errorfmt, rolesstr) };
             return OpResult;
         }
     }
     var r = await rsvc.LoadEntityByKeyAsync(cntx, rid);
     if (r == null)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("db2a3d7bc44d36a9ebeaa0d562c4cd21", "The role is not found.") };
         return OpResult;
     }
     else if (r.RolePriority > maxp.Major)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("67729f0f407d1ea57f28b43235b3e5f6", "Adding more priviledged role is not authorized.") };
         return OpResult;
     }
     List<SimpleMessage> notices = new List<SimpleMessage>();
     var uir = new UsersInRole();
     List<Role> removed = new List<Role>();
     if (DBAutoCleanupRoles)
     {
         // clean up: find more general roles to remove.
         var p = r;
         while (p.ParentID != null)
         {
             p.UpperRef = await rsvc.MaterializeUpperRefAsync(cntx, p);
             p = p.UpperRef;
             foreach (var ur in uroles)
             {
                 if (ur.ID == p.ID)
                 {
                     if (!(from d in removed where d.ID == p.ID select d).Any())
                         removed.Add(p);
                 }
             }
         }
     }
     uir.IsPersisted = false;
     uir.UserID = u.ID;
     uir.RoleID = rid;
     uir.SubPriority = 0;
     uir.AssignDate = DateTime.UtcNow;
     uir.LastModified = uir.AssignDate;
     uir.AdminID = adminId;
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     await uirsvc.AddOrUpdateEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });
     UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
     var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);
     notices.Add(new SimpleMessage
                 {
                     TypeId = 1,
                     Title = string.Format(ResourceUtils.GetString("38015f8af3e032dfd803758dd2bde917", "New role: [{0}] is added.", memb.AcceptLanguages), r.DistinctString),
                     Data = "{ id=\"" + r.ID + "\", type=\"role\", name=\"" + r.DistinctString + "\" }"
                 });
     var _r = new { id = rid, uid = u.ID, name = r.RoleName, path = r.DistinctString, level = uir.SubPriority, op = true };
     List<dynamic> _removed = new List<dynamic>();
     if (removed.Count > 0)
     {
         List<UsersInRole> l = new List<UsersInRole>();
         foreach (var rmv in removed)
         {
             var x = uirsvc.LoadEntityByKey(Cntx, rmv.ID, u.ID);
             l.Add(x);
             _removed.Add(new { id = rmv.ID, name = rmv.RoleName, path = rmv.DistinctString, op = maxp.Major >= rmv.RolePriority });
         }
         await uirsvc.DeleteEntitiesAsync(Cntx, new UsersInRoleSet(), l.ToArray());
         foreach (var _rrmv in removed)
             notices.Add(new SimpleMessage
             {
                 TypeId = 1,
                 Title = string.Format(ResourceUtils.GetString("9708d527fbbf0d9752fc2c741615fb58", "Your role: [{0}] is removed.", memb.AcceptLanguages), _rrmv.DistinctString),
                 Data = "{ id=\"" + _rrmv.ID + "\", type=\"role\", name=\"" + _rrmv.DistinctString + "\" }"
             });
     }
     await AddUserRoleHistory(uir, UserRoleOperations.Added);
     OpResult.Result = new { ok = true, msg = "", added = _r, removed = _removed.ToArray() };
     OpResult.notices = notices.ToArray();
     return OpResult;
 }
 public static async Task<dynamic> ListUsersInRole(string adminId, int id)
 {
     RoleServiceProxy rsvc = new RoleServiceProxy();
     var r = await rsvc.LoadEntityByKeyAsync(Cntx, id);
     List<dynamic> users = new List<dynamic>();
     if (r == null)
         return new { ok = false, msg = ResourceUtils.GetString("2dcb0c4ea3d378571beac6927e1a4a99", "The role is not found!"), users = users };
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     var uirs = await rsvc.MaterializeAllUsersInRolesAsync(Cntx, r);
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     foreach (var uir in uirs)
     {
         uir.User_UserID = await uirsvc.MaterializeUser_UserIDAsync(Cntx, uir);
         uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(Cntx, uir);
         var umax = await MemberAdminContext.GetMaxPriority(uir.User_UserID.ID);
         bool canOp = false;
         if (maxp.Major >= umax.Major)
             canOp = maxp.Major > r.RolePriority || maxp.Major == r.RolePriority && maxp.Minor >= uir.SubPriority;
         users.Add(new { id = uir.RoleRef.ID, uid = uir.User_UserID.ID, name = uir.RoleRef.RoleName, username = uir.User_UserID.Username, path = Utils.GetHtmlRolePath(uir.RoleRef.DistinctString), level = uir.SubPriority, op = canOp });
     }
     return new { ok = true, msg = "", users = users };
 }
 public static async Task<OperationResult> RemoveUserFromRole(string adminId, string uid, int rid)
 {
     OperationResult OpResult = new OperationResult();
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     var cntx = Cntx;
     UserServiceProxy usvc = new UserServiceProxy();
     var u = await usvc.LoadEntityByKeyAsync(cntx, uid);
     if (u == null)
     {
         OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
         return OpResult;
     }
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     var uir = await uirsvc.LoadEntityByKeyAsync(cntx, rid, u.ID);
     if (uir == null)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("78257cace857db766d54e6568d7f912b", "The user is not in this role.") };
         return OpResult;
     }
     uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(cntx, uir);
     if (maxp.Major < uir.RoleRef.RolePriority || maxp.Major == uir.RoleRef.RolePriority && uir.SubPriority > maxp.Major)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("0437b5660f17723dc29c3fa7e08e08a0", "Removing more priviledged role is not authorized.") };
         return OpResult;
     }
     await uirsvc.DeleteEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });
     uir.UserID = u.ID;
     uir.RoleID = rid;
     await AddUserRoleHistory(uir, UserRoleOperations.Deleted);
     UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
     var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);
     OpResult.Result = new { ok = true, msg = "", available = new { id = rid, name = uir.RoleRef.RoleName, path = uir.RoleRef.DistinctString, op = true } };
     OpResult.notices = new SimpleMessage[]
     {
         new SimpleMessage 
         {
             TypeId = 1,
             Title = string.Format(ResourceUtils.GetString("9708d527fbbf0d9752fc2c741615fb58", "Your role: [{0}] is removed.", memb.AcceptLanguages), uir.RoleRef.DistinctString), 
             Data = "{ id=\"" + rid + "\", type=\"role\", name=\"" + uir.RoleRef.DistinctString + "\" }"
         }
     };
     return OpResult;
 }
 public static async Task<OperationResult> AdjustUserRoleLevel(string adminId, string uid, int rid, int del)
 {
     OperationResult OpResult = new OperationResult();
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     var cntx = Cntx;
     UserServiceProxy usvc = new UserServiceProxy();
     var u = usvc.LoadEntityByKey(cntx, uid);
     if (u == null)
     {
         OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
         return OpResult;
     }
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     var uir = await uirsvc.LoadEntityByKeyAsync(cntx, rid, u.ID);
     if (uir == null)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("78257cace857db766d54e6568d7f912b", "The user is not in this role.") };
         return OpResult;
     }
     uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(cntx, uir);
     if (maxp.Major < uir.RoleRef.RolePriority || maxp.Major == uir.RoleRef.RolePriority && uir.SubPriority + del > maxp.Major)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("5986d63fe301793ee7f5b2134a8f8787", "Modifying more priviledged role is not authorized.") };
         return OpResult;
     }
     var oldPrio = uir.SubPriority;
     uir.SubPriority += del;
     uir.LastModified = DateTime.UtcNow;
     uir.AdminID = adminId;
     await uirsvc.AddOrUpdateEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });
     uir.UserID = u.ID;
     uir.RoleID = rid;
     await AddUserRoleHistory(uir, UserRoleOperations.Modified);
     UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
     var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);
     var notice = new SimpleMessage
     {
         TypeId = 1,
         Title = string.Format(ResourceUtils.GetString("54da39696e8014b5ded7a0eaeac1dfc4", "The relative priority of your role: [{0}] is changed from {1} to {2}.", memb.AcceptLanguages),
                               uir.RoleRef.DistinctString, oldPrio, uir.SubPriority),
         Data = "{ id=\"" + rid + "\", type=\"role\", name=\"" + uir.RoleRef.DistinctString + "\" }"
     };
     OpResult.Result = new { ok = true, msg = "" };
     OpResult.notices = new SimpleMessage[] { notice };
     return OpResult;
 }
 public static async Task<string> GetManagedUsers(string adminId, string set, string qexpr, string prevlast)
 {
     var maxp = await GetMaxPriority(adminId);
     JavaScriptSerializer jser = new JavaScriptSerializer();
     dynamic sobj = jser.DeserializeObject(set) as dynamic;
     EntitySetType type;
     if (Enum.TryParse<EntitySetType>(sobj["set"], out type))
     {
         switch (type)
         {
             case EntitySetType.User:
                 {
                     DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(QueryExpresion));
                     DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(User));
                     var ser3 = new JavaScriptSerializer();
                     System.IO.MemoryStream strm = new System.IO.MemoryStream();
                     byte[] sbf = System.Text.Encoding.UTF8.GetBytes(qexpr);
                     strm.Write(sbf, 0, sbf.Length);
                     strm.Position = 0;
                     var _qexpr = ser1.ReadObject(strm) as QueryExpresion;
                     UserServiceProxy svc = new UserServiceProxy();
                     UserSet _set = new UserSet();
                     _set.PageBlockSize = int.Parse(sobj["pageBlockSize"]);
                     _set.PageSize_ = int.Parse(sobj["pageSize"]);
                     if (sobj.ContainsKey("setFilter"))
                         _set.SetFilter = sobj["setFilter"];
                     User _prevlast = null;
                     if (!string.IsNullOrEmpty(prevlast))
                     {
                         strm = new System.IO.MemoryStream();
                         sbf = System.Text.Encoding.UTF8.GetBytes(prevlast);
                         strm.Write(sbf, 0, sbf.Length);
                         strm.Position = 0;
                         _prevlast = ser2.ReadObject(strm) as User;
                     }
                     RoleServiceProxy rsvc = new RoleServiceProxy();
                     var roles = await rsvc.QueryDatabaseAsync(Cntx, new RoleSet(), null);
                     var result = await svc.GetPageItemsAsync(Cntx, _set, _qexpr, _prevlast);
                     var ar = new List<dynamic>();
                     string appId = ApplicationContext.App.ID;
                     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
                     foreach (var e in result)
                     {
                         List<Role> rlist = new List<Role>();
                         foreach (var r in roles)
                         {
                             if (r.RolePriority <= maxp.Major)
                                 rlist.Add(r);
                         }
                         var p = await GetMaxPriority(e);
                         List<dynamic> rolelist = new List<dynamic>();
                         var _roles = await svc.MaterializeAllUsersInRole_UserIDsAsync(Cntx, e);
                         dynamic _max = null;
                         if (_roles != null)
                         {
                             List<UsersInRole> _rlist = new List<UsersInRole>();
                             foreach (var ir in _roles)
                             {
                                 ir.RoleRef = uirsvc.MaterializeRoleRef(Cntx, ir);
                                 _rlist.Add(ir);
                             }
                             foreach (var ir in from d in _rlist orderby d.RoleRef.RolePriority descending, d.SubPriority descending select d)
                             {
                                 bool op = adminId != e.ID && (ir.RoleRef.RolePriority < maxp.Major || ir.RoleRef.RolePriority == maxp.Major && ir.SubPriority <= maxp.Minor);
                                 var _r = new { id = ir.RoleRef.ID, uid = ir.UserID, name = ir.RoleRef.RoleName, path = Utils.GetHtmlRolePath(ir.RoleRef.DistinctString), level = ir.SubPriority, op = op };
                                 if (ir.RoleRef.ID == p.MaxRole.ID)
                                     _max = _r;
                                 rolelist.Add(_r);
                                 int ptr = -1;
                                 for (int i = 0; i < rlist.Count; i++)
                                 {
                                     if (rlist[i].ID == ir.RoleRef.ID)
                                     {
                                         ptr = i;
                                         break;
                                     }
                                 }
                                 if (ptr != -1)
                                     rlist.RemoveAt(ptr);
                             }
                         }
                         List<dynamic> availablers = new List<dynamic>();
                         //if (adminId != e.ID)
                         {
                             foreach (var r in rlist)
                                 availablers.Add(new { id = r.ID, name = r.RoleName, path = Utils.GetHtmlRolePath(r.DistinctString), op = true });
                         }
                         var membs = svc.MaterializeAllUserAppMembers(Cntx, e);
                         ar.Add(new { data = e, member = (from d in membs where d.ApplicationID == appId select d).SingleOrDefault(), roles = rolelist.ToArray(), maxrole = _max, availableRoles = availablers.ToArray(), CanEdit = p.IsLowerOrEqual(maxp) });
                     }
                     string json = ser3.Serialize(ar);
                     return json;
                 }
         }
     }
     return null;
 }
Example #12
0
        public static async Task <string> GetManagedUsers(string adminId, string set, string qexpr, string prevlast)
        {
            var maxp = await GetMaxPriority(adminId);

            JavaScriptSerializer jser = new JavaScriptSerializer();
            dynamic       sobj        = jser.DeserializeObject(set) as dynamic;
            EntitySetType type;

            if (Enum.TryParse <EntitySetType>(sobj["set"], out type))
            {
                switch (type)
                {
                case EntitySetType.User:
                {
                    DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(QueryExpresion));
                    DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(User));
                    var ser3 = new JavaScriptSerializer();
                    System.IO.MemoryStream strm = new System.IO.MemoryStream();
                    byte[] sbf = System.Text.Encoding.UTF8.GetBytes(qexpr);
                    strm.Write(sbf, 0, sbf.Length);
                    strm.Position = 0;
                    var _qexpr            = ser1.ReadObject(strm) as QueryExpresion;
                    UserServiceProxy svc  = new UserServiceProxy();
                    UserSet          _set = new UserSet();
                    _set.PageBlockSize = int.Parse(sobj["pageBlockSize"]);
                    _set.PageSize_     = int.Parse(sobj["pageSize"]);
                    if (sobj.ContainsKey("setFilter"))
                    {
                        _set.SetFilter = sobj["setFilter"];
                    }
                    User _prevlast = null;
                    if (!string.IsNullOrEmpty(prevlast))
                    {
                        strm = new System.IO.MemoryStream();
                        sbf  = System.Text.Encoding.UTF8.GetBytes(prevlast);
                        strm.Write(sbf, 0, sbf.Length);
                        strm.Position = 0;
                        _prevlast     = ser2.ReadObject(strm) as User;
                    }
                    RoleServiceProxy rsvc = new RoleServiceProxy();
                    var roles             = await rsvc.QueryDatabaseAsync(Cntx, new RoleSet(), null);

                    var result = await svc.GetPageItemsAsync(Cntx, _set, _qexpr, _prevlast);

                    var    ar    = new List <dynamic>();
                    string appId = ApplicationContext.App.ID;
                    UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
                    foreach (var e in result)
                    {
                        List <Role> rlist = new List <Role>();
                        foreach (var r in roles)
                        {
                            if (r.RolePriority <= maxp.Major)
                            {
                                rlist.Add(r);
                            }
                        }
                        var p = await GetMaxPriority(e);

                        List <dynamic> rolelist = new List <dynamic>();
                        var            _roles   = await svc.MaterializeAllUsersInRole_UserIDsAsync(Cntx, e);

                        dynamic _max = null;
                        if (_roles != null)
                        {
                            List <UsersInRole> _rlist = new List <UsersInRole>();
                            foreach (var ir in _roles)
                            {
                                ir.RoleRef = uirsvc.MaterializeRoleRef(Cntx, ir);
                                _rlist.Add(ir);
                            }
                            foreach (var ir in from d in _rlist orderby d.RoleRef.RolePriority descending, d.SubPriority descending select d)
                            {
                                bool op = adminId != e.ID && (ir.RoleRef.RolePriority < maxp.Major || ir.RoleRef.RolePriority == maxp.Major && ir.SubPriority <= maxp.Minor);
                                var  _r = new { id = ir.RoleRef.ID, uid = ir.UserID, name = ir.RoleRef.RoleName, path = Utils.GetHtmlRolePath(ir.RoleRef.DistinctString), level = ir.SubPriority, op = op };
                                if (ir.RoleRef.ID == p.MaxRole.ID)
                                {
                                    _max = _r;
                                }
                                rolelist.Add(_r);
                                int ptr = -1;
                                for (int i = 0; i < rlist.Count; i++)
                                {
                                    if (rlist[i].ID == ir.RoleRef.ID)
                                    {
                                        ptr = i;
                                        break;
                                    }
                                }
                                if (ptr != -1)
                                {
                                    rlist.RemoveAt(ptr);
                                }
                            }
                        }
                        List <dynamic> availablers = new List <dynamic>();
                        //if (adminId != e.ID)
                        {
                            foreach (var r in rlist)
                            {
                                availablers.Add(new { id = r.ID, name = r.RoleName, path = Utils.GetHtmlRolePath(r.DistinctString), op = true });
                            }
                        }
                        var membs = svc.MaterializeAllUserAppMembers(Cntx, e);
                        ar.Add(new { data = e, member = (from d in membs where d.ApplicationID == appId select d).SingleOrDefault(), roles = rolelist.ToArray(), maxrole = _max, availableRoles = availablers.ToArray(), CanEdit = p.IsLowerOrEqual(maxp) });
                    }
                    string json = ser3.Serialize(ar);
                    return(json);
                }
                }
            }
            return(null);
        }
 public static async Task<dynamic> RemoveUserFromRole(string adminId, string uid, int rid)
 {
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     UserServiceProxy usvc = new UserServiceProxy();
     var u = await usvc.LoadEntityByKeyAsync(Cntx, uid);
     if (u == null)
         return new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     var uir = await uirsvc.LoadEntityByKeyAsync(Cntx, rid, u.ID);
     if (uir == null)
         return new { ok = false, msg = ResourceUtils.GetString("78257cace857db766d54e6568d7f912b", "The user is not in this role.") };
     uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(Cntx, uir);
     if (maxp.Major < uir.RoleRef.RolePriority || maxp.Major == uir.RoleRef.RolePriority && uir.SubPriority > maxp.Major)
         return new { ok = false, msg = ResourceUtils.GetString("0437b5660f17723dc29c3fa7e08e08a0", "Removing more priviledged role is not authorized.") };
     await uirsvc.DeleteEntitiesAsync(Cntx, new UsersInRoleSet(), new UsersInRole[] { uir });
     uir.UserID = u.ID;
     uir.RoleID = rid;
     await AddUserRoleHistory(uir, UserRoleOperations.Deleted);
     return new { ok = true, msg = "", available = new { id = rid, name = uir.RoleRef.RoleName, path = uir.RoleRef.DistinctString, op = true } };
 }
 public static async Task<dynamic> AdjustUserRoleLevel(string adminId, string uid, int rid, int del)
 {
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     UserServiceProxy usvc = new UserServiceProxy();
     var u = usvc.LoadEntityByKey(Cntx, uid);
     if (u == null)
         return new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     var uir = await uirsvc.LoadEntityByKeyAsync(Cntx, rid, u.ID);
     if (uir == null)
         return new { ok = false, msg = ResourceUtils.GetString("78257cace857db766d54e6568d7f912b", "The user is not in this role.") };
     uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(Cntx, uir);
     if (maxp.Major < uir.RoleRef.RolePriority || maxp.Major == uir.RoleRef.RolePriority && uir.SubPriority + del > maxp.Major)
         return new { ok = false, msg = ResourceUtils.GetString("5986d63fe301793ee7f5b2134a8f8787", "Modifying more priviledged role is not authorized.") };
     uir.SubPriority += del;
     uir.LastModified = DateTime.UtcNow;
     uir.AdminID = adminId;
     await uirsvc.AddOrUpdateEntitiesAsync(Cntx, new UsersInRoleSet(), new UsersInRole[] { uir });
     uir.UserID = u.ID;
     uir.RoleID = rid;
     await AddUserRoleHistory(uir, UserRoleOperations.Modified);
     return new { ok = true, msg = "" };
 }
Example #15
0
        public static async Task <OperationResult> AddUserToRole(string adminId, string uid, int rid)
        {
            OperationResult OpResult = new OperationResult();
            var             maxp     = await MemberAdminContext.GetMaxPriority(adminId);

            RoleServiceProxy rsvc = new RoleServiceProxy();
            UserServiceProxy usvc = new UserServiceProxy();
            var cntx = Cntx;
            var u    = await usvc.LoadEntityByKeyAsync(cntx, uid);

            if (u == null)
            {
                OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
                return(OpResult);
            }
            var uroles = await usvc.MaterializeAllRolesAsync(cntx, u);

            if (DBAutoCleanupRoles)
            {
                // prevent polution
                List <Role> higherroles = new List <Role>();
                foreach (var ur in uroles)
                {
                    var pr = ur;
                    if (pr.ID == rid)
                    {
                        higherroles.Add(ur);
                    }
                    while (pr.ParentID != null)
                    {
                        pr.UpperRef = await rsvc.MaterializeUpperRefAsync(cntx, pr);

                        pr = pr.UpperRef;
                        if (pr.ID == rid)
                        {
                            higherroles.Add(ur);
                            break;
                        }
                    }
                }
                if (higherroles.Count > 0)
                {
                    string rolesstr = "";
                    foreach (var hr in higherroles)
                    {
                        rolesstr += (rolesstr == "" ? "" : ", ") + hr.DistinctString;
                    }
                    string errorfmt = ResourceUtils.GetString("43558b5deaec392b9461d28d4e753687", "Operation denied: the user already has this or more specific roles: '{0}'! Try to remove them before adding present one.");
                    OpResult.Result = new { ok = false, msg = string.Format(errorfmt, rolesstr) };
                    return(OpResult);
                }
            }
            var r = await rsvc.LoadEntityByKeyAsync(cntx, rid);

            if (r == null)
            {
                OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("db2a3d7bc44d36a9ebeaa0d562c4cd21", "The role is not found.") };
                return(OpResult);
            }
            else if (r.RolePriority > maxp.Major)
            {
                OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("67729f0f407d1ea57f28b43235b3e5f6", "Adding more priviledged role is not authorized.") };
                return(OpResult);
            }
            List <SimpleMessage> notices = new List <SimpleMessage>();
            var         uir     = new UsersInRole();
            List <Role> removed = new List <Role>();

            if (DBAutoCleanupRoles)
            {
                // clean up: find more general roles to remove.
                var p = r;
                while (p.ParentID != null)
                {
                    p.UpperRef = await rsvc.MaterializeUpperRefAsync(cntx, p);

                    p = p.UpperRef;
                    foreach (var ur in uroles)
                    {
                        if (ur.ID == p.ID)
                        {
                            if (!(from d in removed where d.ID == p.ID select d).Any())
                            {
                                removed.Add(p);
                            }
                        }
                    }
                }
            }
            uir.IsPersisted  = false;
            uir.UserID       = u.ID;
            uir.RoleID       = rid;
            uir.SubPriority  = 0;
            uir.AssignDate   = DateTime.UtcNow;
            uir.LastModified = uir.AssignDate;
            uir.AdminID      = adminId;
            UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
            await uirsvc.AddOrUpdateEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });

            UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
            var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);

            notices.Add(new SimpleMessage
            {
                TypeId = 1,
                Title  = string.Format(ResourceUtils.GetString("38015f8af3e032dfd803758dd2bde917", "New role: [{0}] is added.", memb.AcceptLanguages), r.DistinctString),
                Data   = "{ id=\"" + r.ID + "\", type=\"role\", name=\"" + r.DistinctString + "\" }"
            });
            var            _r       = new { id = rid, uid = u.ID, name = r.RoleName, path = r.DistinctString, level = uir.SubPriority, op = true };
            List <dynamic> _removed = new List <dynamic>();

            if (removed.Count > 0)
            {
                List <UsersInRole> l = new List <UsersInRole>();
                foreach (var rmv in removed)
                {
                    var x = uirsvc.LoadEntityByKey(Cntx, rmv.ID, u.ID);
                    l.Add(x);
                    _removed.Add(new { id = rmv.ID, name = rmv.RoleName, path = rmv.DistinctString, op = maxp.Major >= rmv.RolePriority });
                }
                await uirsvc.DeleteEntitiesAsync(Cntx, new UsersInRoleSet(), l.ToArray());

                foreach (var _rrmv in removed)
                {
                    notices.Add(new SimpleMessage
                    {
                        TypeId = 1,
                        Title  = string.Format(ResourceUtils.GetString("9708d527fbbf0d9752fc2c741615fb58", "Your role: [{0}] is removed.", memb.AcceptLanguages), _rrmv.DistinctString),
                        Data   = "{ id=\"" + _rrmv.ID + "\", type=\"role\", name=\"" + _rrmv.DistinctString + "\" }"
                    });
                }
            }
            await AddUserRoleHistory(uir, UserRoleOperations.Added);

            OpResult.Result  = new { ok = true, msg = "", added = _r, removed = _removed.ToArray() };
            OpResult.notices = notices.ToArray();
            return(OpResult);
        }