public async Task <IActionResult> Create([Bind("OrganizationId,Name,StreetName,StreetNumber,City,Country,ContactPersonId,PhoneNumber,Email,PathLogoImage")] Organization organization)
        {
            if (ModelState.IsValid)
            {
                organization.OrganizationId = Guid.NewGuid();

                Guid?guidId = null;
                if (_signInManager.IsSignedIn(User))
                {
                    var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                    guidId = Guid.Parse(userId);
                }
                organization.ContactPersonId = (Guid)guidId;

                _context.Add(organization);

                Person person = _context.Persons.SingleOrDefault(x => x.PersonId == organization.ContactPersonId);
                person.OrganizationId = organization.OrganizationId;

                _context.Update(person);

                //Går in i AuthService för att lägga till en roll till en viss användare. I detta fall "SuperCompanyUser" till de som registrerar sig som ansvariga för ett företag
                bool addRole = await _auth.AddRole("SuperCompanyUser");

                var addRoleToUser = await _auth.AddRoleToUser("SuperCompanyUser", person.PersonId.ToString());


                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ContactPersonId"] = new SelectList(_context.Persons, "PersonId", "PersonId", organization.ContactPersonId);
            return(View(organization));
        }
Beispiel #2
0
        public async Task <IActionResult> AddRoleForUser(AddRoleVm addrole)
        {
            bool Userexist = await _auth.UserExist(addrole.Admin.Email);

            //ModelState.AddModelError(addrole.Admin.Email);

            if (!ModelState.IsValid)
            {
                return(View("index"));
            }
            else
            {
                if (Userexist)
                {
                    bool roleExist = await _auth.RoleExist(addrole.Admin.Role);

                    if (!roleExist)
                    {
                        await _auth.CreateRole(addrole.Admin.Role);
                    }

                    await _auth.AddRoleToUser(addrole);

                    return(View("SuccessAddRole", addrole));
                }
                else
                {
                    ModelState.AddModelError("UserDontExist", $"User with email {addrole.Admin.Email} doesn't exist");
                    return(View("index"));
                }
            }
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                Guid myGuid = Guid.NewGuid();

                Input.Person.PersonId = myGuid;

                var user = new ApplicationUser {
                    UserName = Input.Email, Email = Input.Email, Id = myGuid, PersonId = myGuid, Person = Input.Person
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    bool addRole = await _auth.AddRole("CompanyUser");

                    var addRoleToUser = await _auth.AddRoleToUser("CompanyUser", Input.Person.PersonId.ToString());

                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _signInManager.SignInAsync(user, isPersistent : false);



                    //Kontrollera ifall användare verkligen tillhör företaget!
                    var             xy        = Input.Organization.OrganizationId;
                    Person          p1        = _applicationDbContext.Persons.Single(x => x.OrganizationId == xy);
                    ApplicationUser userEmail = await _userManager.FindByIdAsync(p1.PersonId.ToString());

                    string email = userEmail.Email;

                    await _emailSender.SendEmailAsync(email, "Confirm your collugue",
                                                      $"Please confirm {user.Email} as your collugue by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Beispiel #4
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                Guid myGuid = Guid.NewGuid();
                Input.Person.PersonId = myGuid;

                //fyll på med personINFO här!!
                string   name            = info.Principal.Identity.Name;
                string[] first_last_name = name.Split(' ');

                Input.Person.FirstName = first_last_name[0];
                Input.Person.LastName  = first_last_name[1];

                var user = new ApplicationUser {
                    UserName = Input.Email, Email = Input.Email, Id = myGuid, PersonId = myGuid, Person = Input.Person
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    bool addRole = await _auth.AddRole("User");

                    var addRoleToUser = await _auth.AddRoleToUser("User", Input.Person.PersonId.ToString());

                    result = await _userManager.AddLoginAsync(user, info);

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

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Beispiel #5
0
 public async Task <IActionResult> AddRoleForUser(AddRoleVm addrole)
 {
     if (!ModelState.IsValid)
     {
         return(View());
     }
     else if (!await _auth.UserExist(addrole.Email))
     {
         ModelState.AddModelError("UserExist", "Användaren hittades inte");
         return(View());
     }
     else if (!await _auth.RoleExists(addrole.Role))
     {
         ModelState.AddModelError("RoleExist", "Rollen hittades inte");
         return(View());
     }
     else if (!await _auth.AddRoleToUser(addrole.Email, addrole.Role))
     {
         ModelState.AddModelError("CombinationExists", "Användaren har redan denna roll");
         return(View());
     }
     return(View("Success", addrole));
 }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                Guid myGuid = Guid.NewGuid();

                Input.Person.PersonId = myGuid;

                var user = new ApplicationUser {
                    UserName = Input.Email, Email = Input.Email, Id = myGuid, PersonId = myGuid, Person = Input.Person
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    bool addRole = await _auth.AddRole("User");

                    var addRoleToUser = await _auth.AddRoleToUser("User", Input.Person.PersonId.ToString());

                    _logger.LogInformation("User created a new account with password.");
                    //await _dataAccessQuestions.GetRespondersToMarketResearchFromRegistredUser(user.Id); //Kan man göra såhär?
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Beispiel #7
0
        public async Task <IActionResult> AddRoleForUser(AddRoleVm addrole)
        {
            if (!ModelState.IsValid)
            {
                return(View("Index"));
            }

            bool userExist = await _auth.UserExist(addrole.Email);

            if (!userExist)
            {
                ModelState.AddModelError("InvalidUser", "The user does not exist");
                return(View("Index"));
            }

            await _auth.AddRoleToUser(addrole);

            return(View("SuccessAddRole", addrole));
        }
        public async Task <IActionResult> AddRoleForUser(AddRoleVm addrole)
        {
            bool userExist = await _auth.UserExist(addrole.Email);

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("UserDontExist", $"User with email {addrole.Email} does not exist");
                return(View("Index"));
            }

            bool roleExist = await _auth.RoleExist(addrole.RoleName);

            if (roleExist == false)
            {
                await _auth.CreateRole(addrole.RoleName);
            }

            await _auth.AddRoleToUser(addrole.Email, addrole.RoleName);

            return(View("SuccessAddRole"));
        }
Beispiel #9
0
        public async Task <IActionResult> AddRoleForUser(AddRoleVm addrole)
        {
            await _auth.AddRoleToUser(addrole);

            return(View("SuccesAddRole", addrole));
        }