public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            NHibernate.ISession Database = NhibernateBootStrapper.GetSession();
            try
            {
                string header = OperationContext.Current.IncomingMessageHeaders.GetHeader <string>("application-name", "http://aspensys.com/");

                // auth hack - daquinohd
                // ServiceSecurityContext.PrimaryIdentity.Name is not returning a value, so I'm
                // using System.Security.Principal.WindowsIdentity.GetCurrent().Name to get the AppPool Identity for now.
                string name = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
                name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

                UnauthorizedAccessException uae = new UnauthorizedAccessException(string.Format("{0} cannot access the application {1}", name, header));
                if (!OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.IsAuthenticated)
                {
                    FaultException faultException = new FaultException(string.Format("{0} cannot access the application {1}, it is unauthenticated", name, header));
                }
                Account account = Database.Query <Account>().FirstOrDefault <Account>((Account acc) => acc.Username.ToLower() == name.ToLower());
                if (account == null)
                {
                    throw new FaultException(string.Format("{0} cannot access the application {1}, it does not have acces to GUAM", name, header));
                }
                IQueryable <Application> app_auth =
                    from app in Database.Query <Application>()
                    join acc in Database.Query <ApplicationAccount>() on app.ApplicationID equals acc.Application.ApplicationID
                    where acc.Account.AccountID == account.AccountID
                    select app;
                if ((account.Admin ? false : !app_auth.Any <Application>()))
                {
                    throw uae;
                }
                GlobalUsersService instance = instanceContext.GetServiceInstance() as GlobalUsersService;
                if (instance != null)
                {
                    instance.CurrentApplication = (
                        from a in Database.Query <Application>()
                        where a.ApplicationName.ToLower() == header.ToLower()
                        select a).FirstOrDefault <Application>();
                    if (instance.CurrentApplication == null)
                    {
                        throw new Exception(string.Concat("Application '", header, "' not found in database."));
                    }
                }
            }
            finally
            {
                if (Database != null)
                {
                    Database.Dispose();
                }
            }
            return(null);
        }
Esempio n. 2
0
        public static ApplicationInformation ToApplicationInformation(this Application aa)
        {
            ApplicationInformation applicationInformation;
            ISession Database = NhibernateBootStrapper.GetSession();

            try
            {
                ApplicationInformation ai = aa.Copy <ApplicationInformation>();
                ai.UserCount = (
                    from u in Database.Query <GlobalUsers.Entities.User>()
                    where u.Application.ApplicationID == aa.ApplicationID
                    select u).Count <GlobalUsers.Entities.User>();
                ai.Roles     = aa.Roles.ToDictionary <Role, int, string>((Role r) => r.RoleID, (Role r) => r.Name);
                ai.Questions = (
                    from q in aa.Questions.ToList <Question>()
                    select q.Copy <ApplicationQuestion>()).ToArray <ApplicationQuestion>();
                ai.TrustedApplications = aa.ApplicationTrusts.ToDictionary <ApplicationTrust, int, string>((ApplicationTrust t) => t.TrustedApplication.ApplicationID, (ApplicationTrust t) => t.TrustedApplication.ApplicationName);
                if (!(aa.ApplicationName == "ApplicationManagement"))
                {
                    ai.Accounts = (
                        from app_acc in aa.ApplicationAccounts
                        where app_acc.Application.ApplicationID == aa.ApplicationID
                        select app_acc.Account.Username).ToArray <string>();
                }
                else
                {
                    ai.Accounts = (
                        from a in Database.Query <Account>()
                        where a.Admin
                        select a.Username).ToArray <string>();
                }
                applicationInformation = ai;
            }
            finally
            {
                if (Database != null)
                {
                    Database.Dispose();
                }
            }
            return(applicationInformation);
        }