예제 #1
0
        public UserView Create([FromBody] LoginRequest login)
        {
            if (string.IsNullOrWhiteSpace(login.Username) || string.IsNullOrWhiteSpace(login.Password))
            {
                return(null);
            }

            using (var session = NHibernateHelper.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var user = session.QueryOver <User>().Where(x => x.Name.IsInsensitiveLike(login.Username)).SingleOrDefault();
                    if (user != null)
                    {
                        return(null);
                    }
                    user = new User {
                        Name = login.Username, HashedPassword = Hash.getHashSha256(login.Password)
                    };
                    session.Save(user);
                    transaction.Commit();
                    return(new UserView(user));
                }
        }
예제 #2
0
 public IList <OrganizationDetailView> ChangeCountry([FromBody] OrganizationCountryRequest request)
 {
     using (var session = NHibernateHelper.OpenSession())
         using (var transaction = session.BeginTransaction())
         {
             if (string.IsNullOrWhiteSpace(request.CountryCode) ||
                 Country.Countries.ContainsKey(request.CountryCode))
             {
                 foreach (var organizationId in request.OrganizationIds)
                 {
                     var organization = session.QueryOver <Organization>().Where(x => x.Id == organizationId).SingleOrDefault();
                     if (organization != null && organization.CountryCode != request.CountryCode)
                     {
                         organization.CountryCode = request.CountryCode;
                         session.Update(organization);
                     }
                 }
                 transaction.Commit();
             }
             return(session.QueryOver <Organization>().Fetch(x => x.Aliases).Eager
                    .TransformUsing(Transformers.DistinctRootEntity).List().Select(x => new OrganizationDetailView(x))
                    .ToList());
         }
 }
예제 #3
0
        public bool Login([FromBody] LoginRequest login)
        {
            if (string.IsNullOrWhiteSpace(login?.Username) || string.IsNullOrWhiteSpace(login.Password))
            {
                return(false);
            }
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var user = session.QueryOver <User>()
                               .Where(x => x.Name.IsInsensitiveLike(login.Username) && x.HashedPassword == Hash.getHashSha256(login.Password))
                               .SingleOrDefault();
                    if (user == null)
                    {
                        return(false);
                    }

                    user.LastLogin = DateTime.Now;
                    session.Update(user);
                    transaction.Commit();

                    var claims = new List <Claim>();
                    claims.Add(new Claim(ClaimTypes.Name, user.Name));
                    claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
                    //claims.Add(new Claim(ClaimTypes.Email, "*****@*****.**"));
                    var roles = session.QueryOver <UserRole>().Where(x => x.User == user && x.Organization == null).List();
                    claims.AddRange(roles.Select(userRole => new Claim(ClaimTypes.Role, userRole.Role.ToString())));
                    var id = new ClaimsIdentity(claims, "ApplicationCookie");
                    var authenticationManager = Request.GetOwinContext().Authentication;
                    authenticationManager.SignOut("ApplicationCookie");
                    authenticationManager.SignIn(id);
                    return(true);
                }
            }
        }
예제 #4
0
        public IList <OrganizationDetailView> UploadOrganizations()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var organizations = session.QueryOver <Organization>().List();
                using (var parser = new TextFieldParser(Request.Content.ReadAsStreamAsync().Result))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters("\t", ";");
                    parser.HasFieldsEnclosedInQuotes = true;
                    parser.TrimWhiteSpace            = true;
                    while (!parser.EndOfData)
                    {
                        //Process row
                        try
                        {
                            var fields = parser.ReadFields();
                            if (fields == null || fields.Length < 1 || string.IsNullOrWhiteSpace(fields[0]))
                            {
                                continue;
                            }

                            var organization = organizations.SingleOrDefault(x =>
                                                                             string.Equals(x.Name, fields[0], StringComparison.InvariantCultureIgnoreCase) ||
                                                                             x.Aliases.Any(alias =>
                                                                                           string.Equals(alias, fields[0], StringComparison.InvariantCultureIgnoreCase)));
                            var aliasIndex = 1;
                            while (organization == null && aliasIndex < fields.Length)
                            {
                                organization = organizations.SingleOrDefault(x =>
                                                                             string.Equals(x.Name, fields[aliasIndex],
                                                                                           StringComparison.InvariantCultureIgnoreCase) ||
                                                                             x.Aliases.Any(alias =>
                                                                                           string.Equals(alias, fields[aliasIndex],
                                                                                                         StringComparison.InvariantCultureIgnoreCase)));
                                aliasIndex++;
                            }

                            if (organization == null)
                            {
                                organization = new Organization
                                {
                                    Name = fields[0]
                                };
                                organizations.Add(organization);
                                using (var transaction = session.BeginTransaction())
                                {
                                    session.Save(organization);
                                    transaction.Commit();
                                }
                            }

                            organization.Name = fields[0];
                            for (aliasIndex = 1; aliasIndex < fields.Length; aliasIndex++)
                            {
                                if (string.IsNullOrWhiteSpace(fields[aliasIndex]))
                                {
                                    continue;
                                }
                                if (!organization.Aliases.Any(alias =>
                                                              string.Equals(alias, fields[aliasIndex],
                                                                            StringComparison.InvariantCultureIgnoreCase)))
                                {
                                    organization.Aliases.Add(fields[aliasIndex]);
                                }
                            }

                            using (var transaction = session.BeginTransaction())
                            {
                                session.Update(organization);
                                transaction.Commit();
                            }
                        }
                        catch (MalformedLineException ex)
                        {
                            //parser.ErrorLine;
                            //parser.ErrorLineNumber;
                        }
                    }
                }

                return(session.QueryOver <Organization>().Fetch(x => x.Aliases).Eager
                       .TransformUsing(Transformers.DistinctRootEntity).List().Select(x => new OrganizationDetailView(x))
                       .ToList());
            }
        }