public async Task <IActionResult> Registrate([FromForm] RegistrationViewModel model)
        {
            RegistrationValidator validator = new RegistrationValidator(_userManager);
            var results = validator.Validate(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return(View("Registration", model));
            }

            List <KeyValuePair <string, ProductRoleEnum> > products = null;

            if (!string.IsNullOrEmpty(model.ProductKey) && !string.IsNullOrEmpty(model.Role))
            {
                products = new List <KeyValuePair <string, ProductRoleEnum> >()
                {
                    new KeyValuePair <string, ProductRoleEnum>(model.ProductKey,
                                                               (ProductRoleEnum)Int32.Parse(model.Role))
                };
            }

            _userManager.AddUser(model.Username, null, null,
                                 HashComputer.ComputePasswordHash(model.Password), false, products);
            await Authenticate(model.Username, true);

            if (!string.IsNullOrEmpty(model.TicketId))
            {
                _ticketManager.RemoveTicket(Guid.Parse(model.TicketId));
            }

            return(RedirectToAction("Index", "Home"));
        }
        public async void Invite([FromBody] InviteViewModel model)
        {
            InviteValidator validator = new InviteValidator();
            var             results   = await validator.ValidateAsync(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataInviteErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return;
            }

            var ticket = new RegistrationTicket()
            {
                ExpirationDate = DateTime.UtcNow + TimeSpan.FromMinutes(30),
                ProductKey     = model.ProductKey,
                Role           = model.Role
            };

            _ticketManager.AddTicket(ticket);

            var(server, port, login, password, fromEmail) = GetMailConfiguration();

            EmailSender sender = new EmailSender(server,
                                                 string.IsNullOrEmpty(port) ? null : Int32.Parse(port),
                                                 login, password, fromEmail, model.Email);

            var link = GetLink(ticket.Id.ToString());

            Task.Run(() => sender.Send("Invitation link HSM", link));
        }
        public void AddUserRight([FromBody] UserRightViewModel model)
        {
            UserRightValidator validator = new UserRightValidator();
            var results = validator.Validate(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataUserErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return;
            }

            var user = _userManager.GetUser(Guid.Parse(model.UserId));
            var pair = new KeyValuePair <string, ProductRoleEnum>(model.ProductKey, (ProductRoleEnum)model.ProductRole);

            if (user.ProductsRoles == null || !user.ProductsRoles.Any())
            {
                user.ProductsRoles = new List <KeyValuePair <string, ProductRoleEnum> > {
                    pair
                }
            }
            ;
            else
            {
                user.ProductsRoles.Add(pair);
            }

            _userManager.UpdateUser(user);
        }
        public void AddExtraKey([FromBody] ExtraKeyViewModel model)
        {
            ExtraKeyValidator validator = new ExtraKeyValidator(_productManager);
            var results = validator.Validate(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataKeyErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return;
            }

            Product product = _productManager.GetProductCopyByKey(model.ProductKey);

            model.ExtraProductKey = KeyGenerator.GenerateExtraProductKey(
                product.Name, model.ExtraKeyName);

            var extraProduct = new ExtraProductKey(model.ExtraKeyName, model.ExtraProductKey);

            if (product.ExtraKeys == null || product.ExtraKeys.Count == 0)
            {
                product.ExtraKeys = new List <ExtraProductKey> {
                    extraProduct
                }
            }
            ;
            else
            {
                product.ExtraKeys.Add(extraProduct);
            }

            _productManager.UpdateProduct(product);
        }
        public void CreateProduct([FromQuery(Name = "Product")] string productName)
        {
            NewProductNameValidator validator = new NewProductNameValidator(_productManager);
            var results = validator.Validate(productName);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return;
            }

            TempData.Remove(TextConstants.TempDataErrorText);
            _productManager.AddProduct(productName);
        }
示例#6
0
        public void CreateProduct([FromQuery(Name = "Product")] string productName)
        {
            NewProductNameValidator validator = new NewProductNameValidator(_monitoringCore);
            var results = validator.Validate(productName);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return;
            }

            TempData.Remove(TextConstants.TempDataErrorText);
            _monitoringCore.AddProduct(HttpContext.User as User, productName,
                                       out Product newProduct, out string error);
        }
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Authenticate([FromForm] LoginViewModel model)
        {
            LoginValidator validator = new LoginValidator(_userManager);
            var            results   = validator.Validate(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return(RedirectToAction("Index", "Home"));
            }

            TempData.Remove(TextConstants.TempDataErrorText);
            await Authenticate(model.Username, model.KeepLoggedIn);

            return(RedirectToAction("Index", "Home"));
        }
        public void CreateUser([FromBody] UserViewModel model)
        {
            UserValidator validator = new UserValidator(_userManager);
            var           results   = validator.Validate(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataErrorText] = ValidatorHelper.GetErrorString(results.Errors);
            }

            else
            {
                _userManager.AddUser(model.Username, string.Empty, string.Empty,
                                     HashComputer.ComputePasswordHash(model.Password), model.IsAdmin);
            }
        }