Beispiel #1
0
        public virtual List <tbs_Group> GetPage(tbs_Group condition)
        {
            using (SecurityEntities sc = new SecurityEntities())
            {
                var query = from g in sc.tbs_Group select g;

                if (false == string.IsNullOrEmpty(condition.GroupCode))
                {
                    query = query.Where(g => g.GroupCode == condition.GroupCode);
                }
                if (false == string.IsNullOrEmpty(condition.GroupName))
                {
                    query = query.Where(g => 0 == string.Compare(g.GroupName, condition.GroupName, true));
                }
                if (condition.IsActiveParam.HasValue)
                {
                    query = query.Where(g => g.IsActive == condition.IsActiveParam.Value);
                }
                if (false == string.IsNullOrEmpty(condition.Description))
                {
                    query = query.Where(g => g.Description.Contains(condition.Description));
                }

                return(query.Page(condition).ToList());
            }
        }
Beispiel #2
0
        public List <string> GetDeniedMenuItems(string loginName)
        {
            using (SecurityEntities db = new SecurityEntities())
            {
                var allowed      = GetResourcesByPermission("View", loginName);
                var allowedItems = db.tbs_ScreenItem
                                   .Where(a => allowed.Contains(a.AcResourceName) || string.IsNullOrEmpty(a.AcResourceName))
                                   .Select(a => new ScreenItem
                {
                    AcResourceName = a.AcResourceName,
                    ItemSequence   = a.ItemSequence,
                    MenuGroupCode  = a.ManuGroupCode,
                    ScreenCode     = a.ScreenCode,
                    ScreenName     = a.ScreenName,
                    IsSingleton    = a.IsSingleton ?? false,
                })
                                   .ToList();

                var groups = allowedItems
                             .Select(a => a.MenuGroupCode)
                             .ToList();

                var visibleItems = allowedItems
                                   .Where(a => groups.Contains(a.ScreenCode) || false == string.IsNullOrEmpty(a.AcResourceName))
                                   .ToList();

                return(visibleItems.Select(a => a.AcResourceName).ToList());
            }
        }
Beispiel #3
0
        public virtual void UpdateGroupMember(UpdateGroupMemberParam updates)
        {
            using (SecurityEntities sc = new SecurityEntities())
                using (TransactionScope ts = new TransactionScope())
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var m in updates.out_Members)
                    {
                        sb.Append(tbs_UserGroup.GetSqlDelete(m.UserCode, m.GroupCode));
                        sb.AppendLine(";");
                    }
                    foreach (var m in updates.in_Members)
                    {
                        sb.Append(tbs_UserGroup.GetSqlDelete(m.UserCode, m.GroupCode));
                        sb.AppendLine(";");
                    }
                    string sql = sb.ToString();
                    if (false == string.IsNullOrEmpty(sql))
                    {
                        sc.Database.ExecuteSqlCommand(sql);
                    }

                    foreach (var m in updates.in_Members)
                    {
                        sc.tbs_UserGroup.Add(
                            new tbs_UserGroup
                        {
                            GroupCode = m.GroupCode,
                            UserCode  = m.UserCode,
                        });
                        sc.SaveChanges();
                    }
                    ts.Complete();
                }
        }
Beispiel #4
0
        protected List <string> GetResourcesByPermission(string permissionCode, string loginName)
        {
            if (string.IsNullOrEmpty(loginName))
            {
                return(new List <string>());
            }
            using (SecurityEntities db = new SecurityEntities())
            {
                var groupCodes = from ug in db.tbs_UserGroup
                                 join u in db.tbs_User on ug.UserCode equals u.UserCode
                                 where u.LoginName == loginName
                                 select ug.GroupCode;

                var permitGroup = db.tbs_PermissionGroupMap
                                  .Where(a => true == groupCodes.Contains(a.GroupCode));

                var permitUser = from p in db.tbs_PermissionUserMap
                                 join u in db.tbs_User on p.UserCode equals u.UserCode
                                 where u.LoginName == loginName
                                 select p;

                if (permissionCode != "*")
                {
                    permitGroup = permitGroup.Where(a => a.PermissionCode == permissionCode);
                    permitUser  = permitUser.Where(a => a.PermissionCode == permissionCode);
                }

                var resources = permitUser.Select(a => a.AcResourceName)
                                .Union(permitGroup.Select(a => a.AcResourceName))
                                .Distinct()
                                .ToList();

                return(resources);
            }
        }
Beispiel #5
0
        public virtual List <tbs_User> GetPage(tbs_User condition)
        {
            using (SecurityEntities sc = new SecurityEntities())
            {
                var query = from u in sc.tbs_User select u;
                if (false == string.IsNullOrEmpty(condition.GroupCodeParam))
                {
                    query = from u in sc.tbs_User
                            join g in sc.tbs_UserGroup
                            on u.UserCode equals g.UserCode
                            where g.GroupCode == condition.GroupCodeParam
                            select u;
                }

                if (false == string.IsNullOrEmpty(condition.UserCode))
                {
                    query = query.Where(u => u.UserCode == condition.UserCode);
                }
                if (false == string.IsNullOrEmpty(condition.LoginName))
                {
                    query = query.Where(u => 0 == string.Compare(u.LoginName, condition.LoginName, true));
                }
                if (condition.IsActiveParam.HasValue)
                {
                    query = query.Where(u => u.IsActive == condition.IsActiveParam.Value);
                }
                if (false == string.IsNullOrEmpty(condition.Description))
                {
                    query = query.Where(u => u.Description.Contains(condition.Description));
                }

                return(query.Page(condition).ToList());
            }
        }
        public virtual void UpdateScreenPermissions(UpdateScreenPermissionParam updates)
        {
            using (SecurityEntities db = new SecurityEntities())
                using (TransactionScope trans = new TransactionScope())
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var g in updates.out_GroupPermissions)
                    {
                        sb.Append(tbs_PermissionGroupMap.GetSqlDelete(g.AcResourceName, g.GroupCode, g.PermissionCode));
                        sb.AppendLine(";");
                    }
                    foreach (var g in updates.in_GroupPermissions)
                    {
                        sb.Append(tbs_PermissionGroupMap.GetSqlDelete(g.AcResourceName, g.GroupCode, g.PermissionCode));
                        sb.AppendLine(";");
                    }
                    foreach (var u in updates.out_UserPermissions)
                    {
                        sb.Append(tbs_PermissionUserMap.GetSqlDelete(u.AcResourceName, u.UserCode, u.PermissionCode));
                        sb.Append(";");
                    }
                    foreach (var u in updates.in_UserPermissions)
                    {
                        sb.Append(tbs_PermissionUserMap.GetSqlDelete(u.AcResourceName, u.UserCode, u.PermissionCode));
                        sb.Append(";");
                    }
                    string sql = sb.ToString();
                    if (false == string.IsNullOrEmpty(sql))
                    {
                        db.Database.ExecuteSqlCommand(sql);
                    }

                    foreach (var g in updates.in_GroupPermissions)
                    {
                        db.tbs_PermissionGroupMap.Add(
                            new tbs_PermissionGroupMap
                        {
                            AcResourceName = g.AcResourceName,
                            GroupCode      = g.GroupCode,
                            PermissionCode = g.PermissionCode,
                        });
                        db.SaveChanges();
                    }
                    foreach (var g in updates.in_UserPermissions)
                    {
                        db.tbs_PermissionUserMap.Add(
                            new tbs_PermissionUserMap
                        {
                            AcResourceName = g.AcResourceName,
                            UserCode       = g.UserCode,
                            PermissionCode = g.PermissionCode,
                        });
                        db.SaveChanges();
                    }

                    trans.Complete();
                }
        }
Beispiel #7
0
        public List <RestrictedControlItem> GetRestrictedControls(string fullClassname, string loginName)
        {
            string disable  = AccessControlAction.Disable.ToString();
            string hide     = AccessControlAction.Hide.ToString();
            string readOnly = AccessControlAction.ReadOnly.ToString();

            using (SecurityEntities db = new SecurityEntities())
            {
                var resourceNames = db.tbs_RestrictControlItem
                                    .Where(a => a.FullClassName == fullClassname)
                                    .Select(a => a.AcResourceName)
                                    .ToList();

                if (resourceNames.Count == 0)
                {
                    return(new List <RestrictedControlItem>());
                }

                var groupCodes = from ug in db.tbs_UserGroup
                                 join u in db.tbs_User on ug.UserCode equals u.UserCode
                                 where u.LoginName == loginName
                                 select ug.GroupCode;

                var resourceName = resourceNames[0];

                var permGroup = db.tbs_PermissionGroupMap
                                .Where(a => false == groupCodes.Contains(a.GroupCode) &&
                                       a.AcResourceName == resourceName);

                var permUsers = from p in db.tbs_PermissionUserMap
                                join u in db.tbs_User on p.UserCode equals u.UserCode
                                where u.LoginName == loginName && p.AcResourceName == resourceName
                                select p;

                var permissionCodes = (from u in permUsers
                                       join g in permGroup
                                       on u.PermissionCode equals g.PermissionCode
                                       select u.PermissionCode);

                var restricts = db.tbs_RestrictControlItem
                                .Where(a => a.FullClassName == fullClassname &&
                                       permissionCodes.Contains(a.PermissionCode))
                                .Select(a => new RestrictedControlItem
                {
                    ACA = 0 == string.Compare(disable, a.AccessControlAction, true) ? AccessControlAction.Disable :
                          0 == string.Compare(hide, a.AccessControlAction, true) ? AccessControlAction.Hide :
                          0 == string.Compare(readOnly, a.AccessControlAction, true) ? AccessControlAction.ReadOnly :
                          AccessControlAction.None,
                    AcResourceName = a.AcResourceName,
                    ControlId      = a.ControlId,
                    FullClassName  = a.FullClassName,
                    PermissionCode = a.PermissionCode,
                })
                                .ToList();

                return(restricts);
            }
        }
        public virtual List <tbs_ScreenItem> GetScreens(tbs_ScreenItem condition)
        {
            using (SecurityEntities db = new SecurityEntities())
            {
                var screens = db.tbs_ScreenItem
                              .Where(a => false == string.IsNullOrEmpty(a.AcResourceName));

                if (false == string.IsNullOrEmpty(condition.ScreenCode))
                {
                    screens = screens.Where(a => a.ScreenCode.Contains(condition.ScreenCode));
                }
                if (false == string.IsNullOrEmpty(condition.ScreenName))
                {
                    screens = screens.Where(a => a.ScreenName.Contains(condition.ScreenName));
                }

                var defaultPermis = db.tbs_Permission
                                    .Where(a => a.PermissionCode == "View")
                                    .FirstOrDefault();
                if (null == defaultPermis)
                {
                    defaultPermis = new tbs_Permission {
                        PermissionCode = PermissionCode.View.ToString()
                    }
                }
                ;

                var permis = (from a in db.tbs_ScreenPermission
                              join b in db.tbs_Permission
                              on a.PermissionCode equals b.PermissionCode
                              select new
                {
                    ScreenCode = a.ScreenCode,
                    Permissions = b,
                })
                             .GroupBy(a => a.ScreenCode)
                             .ToDictionary(k => k.Key, v => v.Select(a => a.Permissions).OrderBy(a => a.Sequence).ToList());

                var result = screens.ToList();
                result.ForEach(a =>
                {
                    List <tbs_Permission> v;
                    if (permis.TryGetValue(a.ScreenCode, out v))
                    {
                        a.Permissions = permis[a.ScreenCode];
                    }
                    else
                    {
                        a.Permissions = new List <tbs_Permission>()
                        {
                            defaultPermis
                        }
                    };
                });

                return(result);
            }
        }
 public virtual List <tbs_PermissionGroupMap> GetScreenPermissionGroupMap(string acResourceName)
 {
     using (SecurityEntities db = new SecurityEntities())
     {
         return(db.tbs_PermissionGroupMap
                .Where(a => a.AcResourceName == acResourceName)
                .ToList());
     }
 }
Beispiel #10
0
 public virtual bool Exist(string groupCode, string groupName)
 {
     using (SecurityEntities sc = new SecurityEntities())
     {
         return(sc.tbs_Group
                .Where(g => (0 == string.Compare(g.GroupName, groupName, true) || groupName == null) &&
                       (0 == string.Compare(g.GroupCode, groupCode, true) || groupCode == null))
                .Count() > 0);
     }
 }
Beispiel #11
0
 public virtual bool Exist(string userCode, string LoginName)
 {
     using (SecurityEntities sc = new SecurityEntities())
     {
         return(sc.tbs_User
                .Where(u => (0 == string.Compare(u.LoginName, LoginName, true) || LoginName == null) &&
                       (0 == string.Compare(u.UserCode, userCode, true) || userCode == null))
                .Count() > 0);
     }
 }
Beispiel #12
0
        public List <string> GetAllowAnonymousResources()
        {
            using (SecurityEntities db = new SecurityEntities())
            {
                var allowed = db.tbs_ScreenItem
                              .Where(a => a.AllowAnonymous ?? false)
                              .Select(a => a.AcResourceName)
                              .ToList();

                return(allowed);
            }
        }
Beispiel #13
0
        public virtual List <tbs_User> GetMembers(string groupCode)
        {
            using (SecurityEntities sc = new SecurityEntities())
            {
                var query = from u in sc.tbs_User
                            join ug in sc.tbs_UserGroup on u.UserCode equals ug.UserCode
                            where ug.GroupCode == groupCode
                            select u;

                return(query.ToList());
            }
        }
Beispiel #14
0
 public virtual List <tbs_Group> GetGroups(string userCode)
 {
     using (SecurityEntities sc = new SecurityEntities())
     {
         var groups = (from u in sc.tbs_User
                       join ug in sc.tbs_UserGroup on u.UserCode equals ug.UserCode
                       join g in sc.tbs_Group on ug.GroupCode equals g.GroupCode
                       where u.UserCode == userCode
                       select g).Distinct().ToList();
         return(groups);
     }
 }
Beispiel #15
0
 public virtual void CreateGroup(tbs_Group group)
 {
     using (SecurityEntities sc = new SecurityEntities())
         using (TransactionScope ts = new TransactionScope())
         {
             var       mapper   = ServiceContainer.GetService <IMapper>();
             tbs_Group newGroup = mapper.Map <tbs_Group>(group);
             sc.tbs_Group.Add(newGroup);
             sc.SaveChanges();
             ts.Complete();
         }
 }
Beispiel #16
0
 public virtual void CreateUser(tbs_User user)
 {
     using (SecurityEntities sc = new SecurityEntities())
         using (TransactionScope ts = new TransactionScope())
         {
             var      mapper  = ServiceContainer.GetService <IMapper>();
             tbs_User newUser = mapper.Map <tbs_User>(user);
             newUser.Password = SecurityModelCrypto.HashEncrypt(user.Password);
             sc.tbs_User.Add(newUser);
             sc.SaveChanges();
             ts.Complete();
         }
 }
Beispiel #17
0
 public virtual void UpdateGroup(tbs_Group group)
 {
     using (SecurityEntities sc = new SecurityEntities())
         using (TransactionScope ts = new TransactionScope())
         {
             var       mapper = ServiceContainer.GetService <IMapper>();
             tbs_Group edited = mapper.Map <tbs_Group>(group);
             sc.tbs_Group.Attach(edited);
             sc.Entry(edited).State = System.Data.Entity.EntityState.Modified;
             sc.SaveChanges();
             ts.Complete();
         }
 }
Beispiel #18
0
        public List <string> GetDeniedResources(string loginName)
        {
            using (SecurityEntities db = new SecurityEntities())
            {
                var allowed = GetResourcesByPermission("*", loginName);
                var denied  = db.tbs_ScreenItem
                              .Where(a => false == allowed.Contains(a.AcResourceName))
                              .Select(a => a.AcResourceName)
                              .ToList();

                return(denied);
            }
        }
Beispiel #19
0
 public virtual void DeleteUser(List <string> userCodes)
 {
     if (userCodes.Count > 0)
     {
         var targets = userCodes.ToArray();
         using (SecurityEntities sc = new SecurityEntities())
             using (TransactionScope ts = new TransactionScope())
             {
                 sc.Database.ExecuteSqlCommand(tbs_UserGroup.GetSqlDeleteByUser(targets));
                 sc.Database.ExecuteSqlCommand(tbs_User.GetSqlDelete(targets));
                 ts.Complete();
             }
     }
 }
Beispiel #20
0
        public virtual void UpdateUser(tbs_User user)
        {
            using (SecurityEntities sc = new SecurityEntities())
                using (TransactionScope ts = new TransactionScope())
                {
                    var mapper = ServiceContainer.GetService <IMapper>();

                    var      passList = sc.tbs_User.Where(u => u.UserCode == user.UserCode).Select(u => u.Password).ToList();
                    string   pass     = passList.Count > 0 ? passList[0] : string.Empty;
                    tbs_User edited   = mapper.Map <tbs_User>(user);
                    if (false == edited.Password.Equals(pass) || string.IsNullOrEmpty(pass))
                    {
                        edited.Password = SecurityModelCrypto.HashEncrypt(edited.Password);
                    }

                    sc.tbs_User.Attach(edited);
                    sc.Entry(edited).State = System.Data.Entity.EntityState.Modified;
                    sc.SaveChanges();
                    ts.Complete();
                }
        }
Beispiel #21
0
        public virtual bool ChangePassword(string userCode, string oldPassword, string newPassword)
        {
            using (SecurityEntities db = new SecurityEntities())
            {
                string encryptedOld = SecurityModelCrypto.HashEncrypt(oldPassword);
                var    users        = db.tbs_User
                                      .Where(a => (a.UserCode == userCode) &&
                                             a.Password == encryptedOld)
                                      .ToList();
                if (users.Count != 1)
                {
                    return(false);
                }

                string encryptedNew = SecurityModelCrypto.HashEncrypt(newPassword);
                users[0].Password        = encryptedNew;
                db.Entry(users[0]).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                return(true);
            }
        }