Example #1
0
 internal string[] GetUsersInRole(string rolename, out string msg)
 {
     try
     {
         msg = "";
         var userIds = new UserRoleRepository().GetUserRolesByRoleId(GetRoles().Where(p => String.Compare(p.Name.Trim(), rolename.Trim(), StringComparison.CurrentCultureIgnoreCase) == 0).ToList()[0].RoleId).Select(m => m.UserId).ToList();
         if (!userIds.Any())
         {
             return(null);
         }
         return(new UserRepository().GetUsers().Where(m => userIds.Contains(m.UserId)).Select(q => q.Username).ToArray());
     }
     catch (DbEntityValidationException ex)
     {
         BugManager.LogApplicationBug(ex.StackTrace, ex.Source, ex.Message);
         msg = ex.Message;
         return(null);
     }
     catch (Exception ex)
     {
         BugManager.LogApplicationBug(ex.StackTrace, ex.Source, ex.Message);
         msg = ex.Message;
         return(null);
     }
 }
Example #2
0
 public int[] GetRoleIdsForUser(string username, out string msg)
 {
     try
     {
         msg = "";
         var roleIds = new UserRoleRepository().GetUserRolesByUserId(new UserRepository().GetUser(username).UserId).Select(l => l.RoleId).ToList();
         if (roleIds.Count < 1)
         {
             return(null);
         }
         var roles = GetRoles().Where(m => roleIds.Contains(m.RoleId)).ToList().Select(m => m.RoleId).ToArray();
         return(roles);
     }
     catch (DbEntityValidationException ex)
     {
         msg = ex.Message;
         return(null);
     }
     catch (Exception ex)
     {
         msg = ex.Message;
         return(null);
     }
 }
Example #3
0
        internal int AddUsersToRoles(string[] usernames, string[] roleNames, out string msg)
        {
            try
            {
                var names = String.Join("), lower(", usernames);
                var roles = String.Join("), lower(", roleNames);
                if (names.StartsWith("), lower("))
                {
                    names = names.Substring(2);
                }
                names = names + ")";

                if (roles.StartsWith("), lower("))
                {
                    roles = roles.Substring(2);
                }
                roles = roles + ")";

                var sql1 =
                    String.Format(
                        "Select \"UserId\" FROM   \"EnrollKiosk\".\"User\"  WHERE \"UserName\" in ({0})", names);
                var sql2 =
                    String.Format(
                        "Select \"RoleId\" FROM   \"EnrollKiosk\".\"Role\"  WHERE \"Name\" in ({0})", roles);


                var userIds = _repository.RepositoryContext().Database.SqlQuery <int>(sql1).ToList();
                var roleIds = _repository.RepositoryContext().Database.SqlQuery <int>(sql2).ToList();

                if (!userIds.Any() || roleIds.Any())
                {
                    msg = "Process Failed! Either User list is empty or Role list is empty";
                    return(-1);
                }

                var counter = 0;
                foreach (var item in userIds)
                {
                    foreach (var roleitem in roleIds)
                    {
                        var userRole = new UserRole {
                            UserId = item, RoleId = roleitem
                        };
                        var retVal = new UserRoleRepository().AddUserRole(userRole);
                        if (retVal > 0)
                        {
                            counter++;
                        }
                    }
                }

                msg = "";
                return(counter);
            }
            catch (DbEntityValidationException ex)
            {
                msg = ex.Message;
                return(0);
            }
            catch (Exception ex)
            {
                BugManager.LogApplicationBug(ex.StackTrace, ex.Source, ex.Message);
                msg = ex.Message;
                return(0);
            }
        }