Beispiel #1
0
        /// <summary>
        /// Filters documents that have fields that contain a term (not analyzed).
        /// Similar to term query, except that it acts as a filter
        /// </summary>
        public BaseFilter Term(string field, object term)
        {
            var t = new TermFilter()
            {
                Field = field, Value = term
            };

            return(this.SetDictionary("term", field, term, (d, b) => { b.TermFilter = d; }));
        }
Beispiel #2
0
        /// <summary>
        /// Filters documents that have fields that contain a term (not analyzed).
        /// Similar to term query, except that it acts as a filter
        /// </summary>
        public BaseFilter Term(Expression <Func <T, object> > fieldDescriptor, object term)
        {
            var t = new TermFilter()
            {
                Field = fieldDescriptor, Value = term
            };

            return(this.SetDictionary("term", fieldDescriptor, term, (d, b) => { b.TermFilter = d; }));
        }
Beispiel #3
0
        public Admin LoginAdmin(string userName, string password)
        {
            if (String.IsNullOrWhiteSpace(userName) || String.IsNullOrWhiteSpace(password))
                throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY);

            try
            {
                var elasticClient = GetElasticClient();

                var userNameFilter = new TermFilter() {
                    Field = ConstAdmin.USER_NAME,
                    Value = userName
                };

                var loginFilters = new List<FilterContainer>();
                loginFilters.Add(userNameFilter);

                var loginFilter = new AndFilter();
                loginFilter.Filters = loginFilters;

                var loginResponse = elasticClient.Search<Admin>(s => s
                .Index(ElasticMappingConstants.INDEX_NAME)
                .Type(ElasticMappingConstants.TYPE_ADMIN)
                .Filter(loginFilter)
                .Size(1));

                var admin = new Admin();

                if (loginResponse.Total > 0)
                {
                    foreach (IHit<Admin> hit in loginResponse.Hits)
                    {
                        admin = hit.Source;
                    }
                    if (!PasswordHash.ValidatePassword(password, admin.password, admin.salt))
                        throw new Exception(ErrorConstants.WRONG_PASSWORD);
                    return admin;
                }
                else
                    return null;
                

            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.GetBaseException().Message);
                return null;
            }
        }
        public void AddSuperAdmin()
        {
            var elasticClient = GetElasticClient();

            var termFilter = new TermFilter()
            {
                Field = ConstAdmin.TYPE,
                Value = (short)BillingEnums.USER_TYPE.SUPER_ADMIN
            };

            try
            {
                var response = elasticClient.Search<Admin>(a => a
                                .Index(ElasticMappingConstants.INDEX_NAME)
                                .Type(ElasticMappingConstants.TYPE_ADMIN)
                                .Filter(termFilter)
                                .Take(1));

                if (response.Total == 0)
                {
                    var salt = PasswordHash.GenerateSalt();
                    var admin = new Admin()
                    {
                        id = Guid.NewGuid(),
                        username = AppConstants.SUPER_ADMIN_USER_NAME,
                        salt = salt,
                        password = PasswordHash.CreateHash(AppConstants.SUPER_ADMIN_PASSWORD, salt),
                        type = (int)BillingEnums.USER_TYPE.SUPER_ADMIN,
                        created_at = DateTime.UtcNow
                    };

                    var create = elasticClient.Index<Admin>(admin, i => i
                                    .Index(ElasticMappingConstants.INDEX_NAME)
                                    .Type(ElasticMappingConstants.TYPE_ADMIN));
                    
                }
            }
            catch (Exception e)
            {

                Console.Error.WriteLine(e.GetBaseException().Message);
            }

            
        }
Beispiel #5
0
        public bool CreateAdmin(Admin admin, string userName)
        {
            if (String.IsNullOrWhiteSpace(userName)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY);
            if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.SUPER_ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE);

            var newAdmin = new Admin()
            {
                id = Guid.NewGuid(),
                username = userName,
                type = (int)BillingEnums.USER_TYPE.ADMIN,
                created_at = DateTime.UtcNow
            };

            try
            {
                var elasticClient = GetElasticClient();

                var userNameFilter = new TermFilter()
                {
                    Field = ConstAdmin.USER_NAME,
                    Value = userName
                };

                var adminList = elasticClient.Search<Admin>(s => s
                .Index(ElasticMappingConstants.INDEX_NAME)
                .Type(ElasticMappingConstants.TYPE_ADMIN)
                .Filter(userNameFilter)
                );

                if(adminList.Total > 0)
                {
                    throw new Exception(ErrorConstants.ADMIN_USERNAME_ALREADY_TAKEN);
                }

                var response = elasticClient.Index<Admin>(newAdmin, i => i
                .Index(ElasticMappingConstants.INDEX_NAME)
                .Type(ElasticMappingConstants.TYPE_ADMIN)
                );

                return response.Created;

            }
            catch (Exception e)
            {
                
                Console.Error.WriteLine(e.GetBaseException().Message);
                throw e;
            }
        }