Ejemplo n.º 1
0
        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public async Task <IActionResult> DeleteTemplate(string templateIds)
        {
            try
            {
                if (string.IsNullOrEmpty(templateIds))
                {
                    TempData["Message"] = "Please select templates to delete";
                    return(RedirectToAction("Index"));
                }

                var deletedStatus = await this.PutAsync <int>(HttpUriFactory.GetDeleteStockRequest(this.options.Value.APIUrl), templateIds.Split(","));

                if (deletedStatus > 0)
                {
                    TempData["Message"] = "Deleted successful";
                }
                else
                {
                    TempData["Message"] = TempData["Message"];
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                TempData["Message"] = ex.Message;
                Utilities.LogAppError(this.logger, ex);
                return(RedirectToAction("Index"));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <param name="parametersSearch">The parameters search.</param>
        /// <param name="pageSizeString">The page size string.</param>
        /// <param name="page">The page.</param>
        /// <returns></returns>
        public async Task <IActionResult> Index(string parametersSearch, string pageSizeString, int page)
        {
            int pageSize = 20;

            page = page > 0 ? page : 1;
            if (!string.IsNullOrEmpty(pageSizeString))
            {
                pageSize = int.Parse(pageSizeString);
            }
            StockRequest request = new StockRequest()
            {
                Page = new PagingRequest()
                {
                    PageIndex = page - 1, PageSize = pageSize
                }
            };

            try
            {
                StockListViewModel model = null;
                if (!string.IsNullOrEmpty(parametersSearch))
                {
                    request = JsonConvert.DeserializeObject <StockRequest>(parametersSearch);
                }

                request.Page = new PagingRequest()
                {
                    PageIndex = page - 1, PageSize = pageSize
                };
                var result = await this.PostAsync <BaseModel <Stock> >(HttpUriFactory.GetStocksRequest(this.options.Value.APIUrl), request);

                if (result != null)
                {
                    int pageCount    = (result.TotalCount + pageSize - 1) / pageSize;
                    var nextPage     = page == pageCount ? page : (page + 1);
                    var previousPage = page > 1 ? (page - 1) : page;
                    model           = this.CreateDefaultVM <StockListViewModel, StockRequest>(request, parametersSearch, page, pageSize, nextPage, previousPage, pageCount, "Stock", "Index");
                    model.Templates = result.Models;
                }
                else
                {
                    model = this.CreateDefaultVM <StockListViewModel, StockRequest>(request, parametersSearch, page, pageSize, 1, 1, 1, "Stock", "Index");
                }

                if (TempData["Message"] != null)
                {
                    ViewBag.Message = TempData["Message"];
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                Utilities.LogAppError(this.logger, ex);
                return(View(this.CreateDefaultVM <StockListViewModel, StockRequest>(request, parametersSearch, page, pageSize, 1, 1, 1, "Stock", "Index")));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the roles.
        /// </summary>
        /// <param name="roles">The roles.</param>
        /// <returns></returns>
        private IEnumerable <SelectListItem> GetProducts()
        {
            var result = this.PostAsync <BaseModel <Product> >(HttpUriFactory.GetProductsRequest(this.options.Value.APIUrl), new BaseRequest()).Result;

            if (result != null)
            {
                return(result.Models.Select(r => new SelectListItem
                {
                    Text = r.Name,
                    Value = r.Id.ToString(),
                }));
            }

            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the roles.
        /// </summary>
        /// <returns></returns>
        private IEnumerable <SelectListItem> GetUsers()
        {
            var model = this.PostAsync <BaseModel <AppIdentityUser> >(HttpUriFactory.GetUsersRequest(this.options.Value.APIUrl), new UserRequest()).Result;

            if (model != null)
            {
                return(model.Models.Select(r => new SelectListItem
                {
                    Text = r.UserName,
                    Value = r.Id.ToString(),
                }));
            }

            return(null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Registers the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public async Task <IActionResult> Details(int id)
        {
            try
            {
                var model = await this.GetAsync <Stock>(HttpUriFactory.GetStockByIdRequest(this.options.Value.APIUrl, id));

                if (model == null)
                {
                    ViewBag.Message = TempData["Message"];
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                Utilities.LogAppError(this.logger, ex);
                return(View(null));
            }
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Register(StockRegisterViewModel model, string productSearchString)
        {
            try
            {
                if (string.IsNullOrEmpty(productSearchString))
                {
                    ModelState.AddModelError("Products", "Please select a product");
                    model.Products = this.GetProducts();
                    return(View(model));
                }

                Stock s = new Stock()
                {
                    Quantity  = model.Quantity,
                    Price     = model.Price,
                    ProductId = int.Parse(productSearchString)
                };

                var recordAdded = await this.PutAsync <int>(HttpUriFactory.GetNewStockRequest(this.options.Value.APIUrl), s);

                if (recordAdded > 0)
                {
                    ViewBag.Message = "Added successful";
                }
                else
                {
                    ViewBag.Message = TempData["Message"];
                }

                model.Products = this.GetProducts();
                return(View(model));
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                Utilities.LogAppError(this.logger, ex);
                return(View(model));
            }
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Details(Stock model)
        {
            try
            {
                var result = await this.PutAsync <int>(HttpUriFactory.GetUpdateStockRequest(this.options.Value.APIUrl), model);

                if (result > 0)
                {
                    ViewBag.Message = "Saved successful";
                }
                else
                {
                    ViewBag.Message = TempData["Message"];
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                Utilities.LogAppError(this.logger, ex);
                return(View(null));
            }
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Register(Product model)
        {
            try
            {
                var recordAdded = await this.PutAsync <int>(HttpUriFactory.GetNewProductRequest(this.options.Value.APIUrl), model);

                if (recordAdded > 0)
                {
                    ViewBag.Message = "Added successful";
                }
                else
                {
                    ViewBag.Message = TempData["Message"];
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                Utilities.LogAppError(this.logger, ex);
                return(View(model));
            }
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Register(UserRegisterViewModel user, IFormFile file, string roleSearchString)
        {
            var path = string.Empty;

            try
            {
                bool allValidated = true;
                if (string.IsNullOrEmpty(roleSearchString))
                {
                    ModelState.AddModelError("Roles", "Please select role(s)");
                    allValidated = false;
                }

                // check password
                Regex rgx = new Regex(@"^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&.])[A-Za-z\d@$!%*#?&.]{6,}$");
                if (!rgx.IsMatch(user.Password))
                {
                    ModelState.AddModelError("Password", "Password requires at least 6 characters, contains digit and non-alphanumeric");
                    allValidated = false;
                }

                if (this.UserList != null && this.UserList.Any(s => s.Text.ToLower() == user.UserName.ToLower()))
                {
                    ModelState.AddModelError("UserName", string.Format("This user {0} has been taken", user.UserName));
                    allValidated = false;
                }

                if (!allValidated)
                {
                    user.Roles = this.RoleList;
                    return(View(user));
                }

                roleSearchString = roleSearchString.Remove(roleSearchString.Length - 1);
                RegisterModel model = new RegisterModel()
                {
                    UserName     = user.UserName,
                    Password     = user.Password,
                    Email        = user.Email,
                    IsActive     = user.IsActive,
                    DatabaseName = this.dbName,
                    ImageSource  = user.ImageSource,
                    PhoneNo      = user.PhoneNo,
                    Roles        = roleSearchString.Split(",")
                };

                if (file != null)
                {
                    //repare path for file
                    path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\FileUploads\\" + this.tenantName + "\\" + this.httpContext.User.Identity.Name + "\\avatars");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    string fileName = file.GetFilename();
                    path = Path.Combine(path, fileName);

                    //input image file to system in order load it later
                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);

                        model.ImageSource = "FileUploads/" + this.tenantName + "/" + this.httpContext.User.Identity.Name + "/avatars/" + fileName;
                    }
                }

                var addedStatus = await this.PutAsync <RegisterModelResponse>(HttpUriFactory.GetNewUserRequest(this.options.Value.APIUrl), model);

                if (addedStatus != null && addedStatus.AddedResult == LoginResult.Allowed)
                {
                    ViewBag.Message = "Added successful";
                }
                else
                {
                    ViewBag.Message = TempData["Message"];
                }

                user.Roles = this.RoleList;
                return(View(user));
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                Utilities.LogAppError(this.logger, ex);
                if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }

                user.Roles = this.RoleList;
                return(View(user));
            }
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Login(LoginViewModel loginViewModel)
        {
            try
            {
                var result = await this.PostAsync <JwtResponse>(HttpUriFactory.GetAuthLoginRequest(this.options.Value.APIUrl), loginViewModel);

                if (result != null)
                {
                    string token = Tokens.RefreshToken(result.auth_token);
                    JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
                    JwtSecurityToken        jwtToken     = (JwtSecurityToken)tokenHandler.ReadToken(token);
                    var claims = jwtToken.Claims;
                    claims = claims.Append(new Claim(Constants.AuthTokenFieldName, result.auth_token));
                    claims = claims.Append(new Claim(Constants.TenantFieldName, loginViewModel.TenantName));
                    var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                    var authProperties = new AuthenticationProperties
                    {
                        AllowRefresh = true,
                        //Refreshing the authentication session should be allowed.

                        ExpiresUtc = DateTimeOffset.Now.AddMinutes(result.expires_in),
                        //The time at which the authentication ticket expires.A
                        // value set here overrides the ExpireTimeSpan option of
                        // CookieAuthenticationOptions set with AddCookie.

                        IsPersistent = true,
                        //Whether the authentication session is persisted across
                        // multiple requests.Required when setting the
                        // ExpireTimeSpan option of CookieAuthenticationOptions
                        // set with AddCookie.Also required when setting
                        // ExpiresUtc.

                        IssuedUtc = DateTime.Now,
                        //The time at which the authentication ticket was issued.

                        //RedirectUri = "https://localhost:44341"
                        // The full path or absolute URI to be used as an http
                        // redirect response value.
                    };

                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

                    if (string.IsNullOrEmpty(loginViewModel.ReturnUrl))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                    return(Redirect(loginViewModel.ReturnUrl));
                }
                else
                {
                    ModelState.AddModelError("All", "Incorrect username/password or account is deactivated");
                    return(View(loginViewModel));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("All", ex.Message);
                Utilities.LogAppError(this.logger, ex);
                return(View(loginViewModel));
            }
        }