public User[] GetAllUsers()
        {
            _Trace.TraceEvent(TraceEventType.Verbose, -1, "GetAllUsers");

            UserEntity[] entities = new CriticalResultsEntityManager().GetAllUsers();
            List<User> users = new List<User>();
            foreach (UserEntity e in entities)
            {
                User u = new User(e);
                u.ResolveRoles();
                users.Add(u);
            }
            return users.ToArray();
        }
        public User CreateANCRUser(User user)
        {
            CriticalResultsEntityManager manager = new CriticalResultsEntityManager();
            UserEntity userEntity = manager.CreateUserEntity(user.ToEntity());

            AuditEvent("CreateUser:Success", user.UserName);

            LevelEntity[] levels = manager.GetLevels();
            List<string> levelNames = new List<string>();
            foreach (LevelEntity level in levels)
            {
                levelNames.Add(level.Name);
            }

            TransportEntity[] transports = manager.GetAllTransports();
            foreach (TransportEntity transport in transports)
            {
                bool addTransport = true;
                foreach (UserTransportEntity userTransport in userEntity.UserTransports)
                {
                    if (userTransport.Transport.Name == transport.Name)
                    {
                        addTransport = false;
                    }
                }
                if (addTransport)
                {
                    if (user.Transports != null)
                    {
                        bool transportFound = false;
                        for (int i = 0; i < user.Transports.Length; i++)
                        {
                            if (user.Transports[i].Transport.Name == transport.Name)
                            {
                                if (user.Transports[i].Address == null)
                                {
                                    user.Transports[i].Address = "";
                                }
                                manager.CreateUserTransport(userEntity.UserName, transport.Name, user.Transports[i].Address);
                                manager.UpdateLevelsToUserTransport(user.UserName, transport.Name, user.Transports[i].Address, levelNames.ToArray());
                                transportFound = true;

                            }
                        }
                        if (!transportFound)
                        {
                            manager.CreateUserTransport(userEntity.UserName, transport.Name, "");
                            manager.UpdateLevelsToUserTransport(user.UserName, transport.Name, "", levelNames.ToArray());
                        }
                    }
                    else
                    {
                        manager.CreateUserTransport(userEntity.UserName, transport.Name, "");
                        manager.UpdateLevelsToUserTransport(user.UserName, transport.Name, "", levelNames.ToArray());
                    }
                }
            }

            userEntity = manager.GetUser(user.UserName);

            //bool passwordFound = false;
            //foreach (UserEntryEntity uee in e.UserEntries)
            //{
            //    if (uee.Type == "AuthExt" && uee.Key == "ANCR")
            //        passwordFound = true;
            //}
            //if(!passwordFound)
            //    GeneratePassword(user.UserName);

            User u = new User(userEntity);
            u.ResolveRoles();
            u.ResolveTransports();
            u.ResolveEntries();
            return u;
        }
        public User GetUser_NonWeb(string userName)
        {
            _Trace.TraceEvent(TraceEventType.Verbose, -1, "GetUser_NonWeb");

            UserEntity e = new CriticalResultsEntityManager().GetUser(userName);
            if (e == null)
                return null;
            User u = new User(e);
            u.ResolveRoles();
            u.ResolveTransports();
            u.ResolveProxied();
            u.ResolveProxies();
            u.ResolveEntries();
            foreach (UserTransport t in u.Transports)
            {
                t.ResolveLevels();
            }
            return u;
        }
        public User[] QueryUsers(string queryString, int? pageSize, int? pageNumber)
        {
            _Trace.TraceEvent(TraceEventType.Verbose, -1, "QueryUsers");

            UserEntity[] entities = new CriticalResultsEntityManager().QueryUserEntity(queryString, pageSize, pageNumber);
            List<User> users = new List<User>();
            foreach (UserEntity e in entities)
            {
                if (!e.Roles.IsLoaded)
                    e.Roles.Load();
                User u = new User(e);
                u.ResolveRoles();
                users.Add(u);
            }
            return users.ToArray();
        }
 internal static void AdditionalCheck(string userName, HTTPCheckRoles rolesAllowed)
 {
     HTTPBasicAuthenticationHeader header = HTTPBasicAuthenticationHeader.GetFromWCF();
     if (string.Compare(header.UserName, userName, true) != 0)
     {
         UserEntity e = new CriticalResultsEntityManager().GetUser(userName);
         User u = new User(e);
         u.ResolveRoles();
         bool ok = false;
         foreach (Role r in u.Roles)
         {
             object robj = Enum.Parse(typeof(HTTPCheckRoles), r.Name, true);
             if (robj != null)
             {
                 if (((HTTPCheckRoles)robj | rolesAllowed)!=0)
                 {
                     ok=true;
                     break;
                 }
             }
         }
         if (!ok)
         {
             throw new SecurityException();
         }
     }
 }