public static RegisterOrganizationViewModel GetOrganizationInfoForRegistration()
        {
            RegisterOrganizationViewModel model = new RegisterOrganizationViewModel();

            Console.WriteLine("Введите название организации :");
            model.FullName = Console.ReadLine();
            Console.WriteLine("Введите идентификационный номер организации :");
            model.IdentificationNumber = Console.ReadLine();
            Console.WriteLine("Введите тип организации :");
            model.OrganizationType = Console.ReadLine();
            Console.WriteLine("Введите имя руководителя организации :");
            model.CeoFirstName = Console.ReadLine();
            Console.WriteLine("Введите фамилию руководителя организации :");
            model.CeoLastName = Console.ReadLine();
            Console.WriteLine("Введите email организации :");
            model.CeoEmail = Console.ReadLine();
            Console.WriteLine("Введите дату рождения руководителя организации :");
            model.CeoDoB = DateTime.Parse(Console.ReadLine());
            Console.WriteLine("Введите пароль :");
            model.Password = Console.ReadLine();
            Console.WriteLine("Подтвердите пароль :");
            model.PasswordConfirm = Console.ReadLine();

            return(model);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> RegisterOrganization(RegisterOrganizationViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, UserRole = "Organization"
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    await this.UserManager.AddToRoleAsync(user.Id, user.UserRole);

                    return(RedirectToAction("Create", "NonprofitOrganizations", user));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 3
0
 public ActionResult CreateOrganizationAndCeo(RegisterOrganizationViewModel model)
 {
     if (ModelState.IsValid)
     {
         organizationManagement.OpenOrganization(model);
     }
     return(RedirectToAction("Index"));
 }
Exemplo n.º 4
0
        public async Task <ActionResult> RegisterOrganization(RegisterOrganizationViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var user = new Organization
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    Description = model.Description,
                    Name        = model.Name,
                    Address     = model.Address,
                    CreatedOn   = DateTime.UtcNow,
                    Image       = model.Image
                };

                var result = await this.UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await this.SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    if (!this.RoleManager.RoleExists(RoleConstants.OrganizationRoleConstant))
                    {
                        var role = new IdentityRole {
                            Name = RoleConstants.OrganizationRoleConstant
                        };
                        this.RoleManager.Create(role);
                    }

                    await this.UserManager.AddToRoleAsync(user.Id, RoleConstants.OrganizationRoleConstant);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    return(this.RedirectToAction("Index", "Home"));
                }

                this.AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(this.View("Register", model));
        }
        public void OrganizationManagementService_OpenOrganization()
        {
            ApplicationDbContext applicationDb = new ApplicationDbContext();
            IdentityDbContext    identityDb    = new IdentityDbContext();

            applicationDb.Database.CreateIfNotExists();
            identityDb.Database.CreateIfNotExists();

            RegisterOrganizationViewModel model = new RegisterOrganizationViewModel()
            {
                FullName             = "TestCompany_01",
                IdentificationNumber = "22222222",
                OrganizationType     = "ТОО",
                CeoFirstName         = "TestCeo",
                CeoLastName          = "TestCeo",
                CeoEmail             = "*****@*****.**",
                CeoDoB          = new DateTime(1985, 3, 9),
                Password        = "******",
                PasswordConfirm = "test111"
            };

            OrganizationManagementService sut = new OrganizationManagementService();

            sut.OpenOrganization(model);

            Organization org = applicationDb.Organizations
                               .SingleOrDefault(p => p.FullName == "TestCompany_01" && p.IdentificationNumber == "22222222");
            Employee        emp  = applicationDb.Employees.SingleOrDefault(p => p.FirstName == "TestCeo" && p.LastName == "TestCeo");
            ApplicationUser user = identityDb.ApplicationUsers.SingleOrDefault(p => p.Email == "*****@*****.**");
            ApplicationUserPasswordHistory userPasswordHistory = identityDb.ApplicationUserPasswordHistories
                                                                 .SingleOrDefault(p => p.ApplicationUserId == user.Id && p.Password == "test111");
            ApplicationUserSignInHistory userSignInHistory = identityDb.ApplicationUserSignInHistories
                                                             .SingleOrDefault(p => p.ApplicationUserId == user.Id);

            Assert.IsNotNull(org);
            Assert.IsNotNull(emp);
            Assert.IsNotNull(user);
            Assert.IsNotNull(userPasswordHistory);
            Assert.IsNotNull(userSignInHistory);
        }
Exemplo n.º 6
0
        public async Task <ActionResult> RegisterOrganization(RegisterOrganizationViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.ImageURL == null)
                {
                    model.ImageURL = ControllersConst.DefaultOrganizationImage;
                }

                var user = new Organization {
                    UserName               = model.UserName,
                    Email                  = model.Email,
                    Name                   = model.Name,
                    Description            = model.Description,
                    IsLookingForVolunteers = model.IsLookingForVolunteers,
                    ImageURL               = model.ImageURL,
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, ControllersConst.OrganizationRole);
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public void OpenOrganization(RegisterOrganizationViewModel model)
        {
            var geoLocationInfo = GeoLocationInfo.GetGeolocationInfo();

            if (model == null)
            {
                throw new Exception($"{typeof(RegisterOrganizationViewModel).Name} is null");
            }

            var checkOrganization = _applicationDbContext.Organizations
                                    .SingleOrDefault(p => p.IdentificationNumber == model.IdentificationNumber || p.FullName == model.FullName);

            if (checkOrganization != null)
            {
                throw new Exception("Такая организация уже сущуствует в базе");
            }

            var checkOrganizationType = _applicationDbContext.OrganizationTypes
                                        .SingleOrDefault(p => p.Name == model.OrganizationType);

            if (checkOrganizationType == null)
            {
                OrganizationType orgType = new OrganizationType()
                {
                    Id   = Guid.NewGuid(),
                    Name = model.OrganizationType
                };
                _applicationDbContext.OrganizationTypes.Add(orgType);
                _applicationDbContext.SaveChanges();
                checkOrganizationType = orgType;
            }


            Organization organization = new Organization()
            {
                Id                   = Guid.NewGuid(),
                FullName             = model.FullName,
                IdentificationNumber = model.IdentificationNumber,
                RegistrationDate     = DateTime.Now,
                OrganizationTypeId   = checkOrganizationType.Id
            };

            _applicationDbContext.Organizations.Add(organization);
            _applicationDbContext.SaveChanges();

            var checkEmployeeEmail = _applicationDbContext.Employees.Any(p => p.Email == model.CeoEmail);

            if (!checkEmployeeEmail)
            {
                var ceoPosition = _applicationDbContext.EmployeePositions.SingleOrDefault(p => p.Name == "CEO");
                if (ceoPosition == null)
                {
                    EmployeePosition pos = new EmployeePosition()
                    {
                        Id   = Guid.NewGuid(),
                        Name = "CEO"
                    };
                    _applicationDbContext.EmployeePositions.Add(pos);
                    _applicationDbContext.SaveChanges();
                    ceoPosition = pos;
                }

                Employee employee = new Employee()
                {
                    Id                 = Guid.NewGuid(),
                    FirstName          = model.CeoFirstName,
                    LastName           = model.CeoLastName,
                    DoB                = model.CeoDoB,
                    Email              = model.CeoEmail,
                    EmployeePositionId = new Guid(ceoPosition.Id.ToString()),
                    OrganizationId     = organization.Id
                };
                _applicationDbContext.Employees.Add(employee);
                _applicationDbContext.SaveChanges();

                ApplicationUser user = new ApplicationUser()
                {
                    Id                   = Guid.NewGuid(),
                    Email                = model.CeoEmail,
                    IsActive             = true,
                    FailedSignInCount    = 0,
                    CreatedDate          = DateTime.Now,
                    AssosiatedEmployeeId = employee.Id
                };
                _identityDbContext.ApplicationUsers.Add(user);
                _identityDbContext.SaveChanges();

                ApplicationUserPasswordHistory userPasswordHistory = new ApplicationUserPasswordHistory()
                {
                    Id                = Guid.NewGuid(),
                    SetupDate         = DateTime.Now,
                    Password          = model.Password,
                    ApplicationUserId = user.Id
                };
                _identityDbContext.ApplicationUserPasswordHistories.Add(userPasswordHistory);
                _identityDbContext.SaveChanges();

                ApplicationUserSignInHistory userSignInHistory = new ApplicationUserSignInHistory()
                {
                    Id                = Guid.NewGuid(),
                    SignInTime        = DateTime.Now,
                    MachineIp         = geoLocationInfo.ip,
                    IpToGeoCountry    = geoLocationInfo.country_name,
                    IpToGeoCity       = geoLocationInfo.city,
                    IpToGeoLatitude   = geoLocationInfo.latitude,
                    IpToGeoLongitude  = geoLocationInfo.longitude,
                    ApplicationUserId = user.Id
                };
                _identityDbContext.ApplicationUserSignInHistories.Add(userSignInHistory);
                _identityDbContext.SaveChanges();
            }
        }