public ActionResult AddAgent()
        {
            var model = new AddAgentViewModel();

            model.Languages =
                CultureHelper.GetAvailableAgentCultures()
                .Select(c => new AddLanguageModel {
                Item = new SelectListItem {
                    Value = c.Name, Text = c.NativeName
                }
            })
                .ToList();
            model.Qualifications = Enum.GetValues(typeof(QualificationType))
                                   .Cast <QualificationType>()
                                   .Select(c => new AddQualificationModel {
                QualificationType = c, Selected = false
            })
                                   .ToList();

            model.Branches =
                _agencyService.GetBranches(User.Identity.GetUserId())
                .Select(c => new SelectListItem {
                Value = c.Id.ToString(), Text = c.Name
            })
                .ToList();
            return(View(model));
        }
        public async Task <ActionResult> AddAgent(AddAgentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var applicationUser = new ApplicationUser
                {
                    Email     = model.Email,
                    UserName  = model.Email,
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                };
                var result = await _authManager.CreateAsync(applicationUser, model.Password);

                if (result.Succeeded)
                {
                    var newUser = await _authManager.FindByNameAsync(model.Email);

                    await _agencyService.CreateAgentAsync(User.Identity.GetUserId(), newUser, model);

                    #region Send confirmation Email to agent

                    string code = await _authManager.GenerateEmailConfirmationTokenAsync(newUser.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                                 new { userId = newUser.Id, code = code, area = "" }, protocol: Request.Url.Scheme);
                    var emailModel = new CreateAgentMailViewModel();
                    emailModel.Name            = model.FirstName + " " + model.LastName;
                    emailModel.ConfirmationUrl = callbackUrl;
                    emailModel.Password        = model.Password;
                    emailModel.Email           = model.Email;
                    var mailContent = EmailSender.GetRazorViewAsString(emailModel,
                                                                       "~/Views/EmailTemplates/CreateAgent.cshtml");
                    try
                    {
                        await _authManager.SendEmailAsync(userId : newUser.Id, subject : Resource.Email_Register_Mail_Title, body : mailContent);
                    }
                    catch (Exception)
                    {
                        this.AddToastMessage(Resource.Error, Resource.An_error_occurred_while_processing_your_request, ToastType.Error);
                    }
                    #endregion

                    var roleResult = await _authManager.AddUserToRoleAsync(newUser.Id, RoleDefinitions.Agent.ToString());

                    if (!roleResult.Succeeded)
                    {
                        roleResult.Errors.ForEach(c => ModelState.AddModelError("", c));
                    }
                    return(RedirectToAction("Step3", "Agency", new { area = "Agent" }));
                }

                result.Errors.ForEach(c => ModelState.AddModelError("", c));
            }
            return(View(model));
        }
Exemple #3
0
 public Task CreateAgentAsync(string newUserId, int agencyId, AddAgentViewModel model)
 {
     _dbContext.Agents.Add(new Agent
     {
         UserId    = newUserId,
         AgencyId  = agencyId,
         Languages = model.Languages.Where(c => c.Item.Selected).Select(c => new Language {
             LanguageCulture = c.Item.Value, Level = c.LanguageLevel.GetValueOrDefault()
         }).ToList(),
         Qualifications = model.Qualifications.Where(c => c.Selected).Select(c => new Qualification {
             QualificationType = c.QualificationType.GetValueOrDefault()
         }).ToList(),
         Education             = model.Education,
         BranchId              = model.SelectedBranchId,
         FieldOfResponsibility = model.FieldOfResponsibility,
         PositionInCompany     = model.PositionInCompany,
     });
     return(_dbContext.SaveChangesAsync());
 }
        public IActionResult Add(AddAgentViewModel addAgentViewModel)
        {
            if (ModelState.IsValid)
            {
                Agent newAgent = new Agent
                {
                    Name    = addAgentViewModel.Name,
                    Phone   = addAgentViewModel.Phone,
                    Street1 = addAgentViewModel.Street1,
                    Street2 = addAgentViewModel.Street2,
                    City    = addAgentViewModel.City,
                    State   = addAgentViewModel.State,
                    Zip     = addAgentViewModel.Zip
                };

                context.Agents.Add(newAgent);
                context.SaveChanges();

                return(Redirect("/Agent"));
            }
            return(View(addAgentViewModel));
        }
Exemple #5
0
        public ActionResult Add(AddAgentViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Неправильные данные формы создания.");
                return(View());
            }
            var agentManager = new AgentsManager();
            var existedAgent = agentManager
                               .GetAgents()
                               .FirstOrDefault(a => a.IpAddress == model.IpAddress && a.Port == model.Port);

            if (existedAgent != null)
            {
                ModelState.AddModelError("", "Агент уже зарегистрирован.");
                return(View());
            }
            var agentAddress  = string.Format("http://{0}:{1}", model.IpAddress, model.Port);
            var agentResponse = PingAgent(agentAddress);

            if (agentResponse.Equals(AgentPingResponse))
            {
                //var agentSettings = GetAgentConfig(agentAddress);
                var agent = new AgentSettings
                {
                    Status       = AgentStatus.Working,
                    CreationDate = DateTime.UtcNow,
                    IpAddress    = model.IpAddress,
                    Port         = model.Port
                };
                agentManager.Add(agent);
                return(RedirectToAction("Index", "Agents"));
            }
            ModelState.AddModelError("", "Не удалось установить соединение с агентом.");
            return(View());
        }
        public IActionResult Add()
        {
            AddAgentViewModel addAgentViewModel = new AddAgentViewModel();

            return(View(addAgentViewModel));
        }
Exemple #7
0
        public async Task CreateAgentAsync(string managerUserId, ApplicationUser newUser, AddAgentViewModel model)
        {
            var agency = _dbContext.Agencies.FirstOrDefault(c => c.ManagerId.Equals(managerUserId));

            if (agency != null)
            {
                await _agentService.CreateAgentAsync(newUser.Id, agency.Id, model);
            }
        }