예제 #1
0
        public static DocRepositoryContext GetInstance()
        {
            if (instance != null)
            {
                return(instance);
            }

            instance = new DocRepositoryContext();
            return(instance);
        }
예제 #2
0
        private LoginSessionFactory CreateSessionFactory()
        {
            var context     = DocRepositoryContext.GetInstance();
            var currentUser = Membership.GetUser();

            if (string.IsNullOrEmpty(currentUser?.UserName))
            {
                throw new Exception("Authorization Error");
            }

            string password;

            using (var usersContext = new UsersContext())
            {
                password = usersContext.UserProfiles.Where(up => up.UserId == WebSecurity.CurrentUserId).First()
                           .SqlPassword;
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new Exception("Authorization Error");
            }

            if (context.Owners == null)
            {
                context.UpdateOwners();
            }

            if (context.Owners == null)
            {
                throw new Exception("MSSQL did not provide more than one login");
            }

            if (!context.Owners.Contains(currentUser.UserName))
            {
                context.AddDbUser(currentUser.UserName, password);
            }

            var cgf  = new Configuration();
            var data = cgf.Configure(HttpContext.Current.Server.MapPath(@"\Models\NHibernate\Configuration\user.cfg.xml"));

            cgf.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"\Models\NHibernate\Mappings")));
            cgf.SetProperty(
                "connection.connection_string",
                @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AppELMA.mdf;" +
                $"User Id ={currentUser.UserName};Password={password};"
                );
            return(new LoginSessionFactory(data.BuildSessionFactory(), currentUser.UserName));
        }