Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(int companyid)
        {
            var companyId = _userManager.FindByIdAsync(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value).Result.CompanyId;

            EditCompany = await _context.Company.FirstOrDefaultAsync(m => m.Id == companyid);

            if (EditCompany == null)
            {
                return(Redirect(Url.Content("~/Forbidden")));;
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (companyId != companyid)
            {
                EditCompany.CompanyIsEnabled = Input.CompanyIsEnabled;
            }

            EditCompany.CompanyName        = Input.CompanyName;
            EditCompany.CompanySlogan      = Input.CompanySlogan;
            EditCompany.CompanyDescription = Input.CompanyDescription;
            if (Companylogo != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await Companylogo.CopyToAsync(memoryStream);

                    EditCompany.CompanyLogo = memoryStream.ToArray();
                }
            }


            _context.Attach(EditCompany).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(RedirectToPage("/Company/Index"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync(/*string returnUrl = null*/)
        {
            if (ModelState.IsValid)
            {
                //returnUrl = returnUrl ?? Url.Content("~/");

                var company = new Company {
                    CompanyName = Company.CompanyName, CompanyDescription = Company.CompanyDescription, CompanySlogan = Company.CompanySlogan, CompanyIsEnabled = Company.CompanyIsEnabled, CreationDate = DateTimeOffset.Now
                };
                // convert companyLogo IFormfile to byte array

                if (Companylogo != null)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await Companylogo.CopyToAsync(memoryStream);

                        Company.CompanyLogo = memoryStream.ToArray();
                    }
                }
                _context.Company.Add(company);
                await _context.SaveChangesAsync();

                var user = new AppUser {
                    CompanyId = _context.Company.FirstOrDefaultAsync(m => m.CompanyName == Company.CompanyName).Result.Id, FirstName = NewUser.FirstName, LastName = NewUser.LastName, UserName = NewUser.Email, Email = NewUser.Email, CreationDate = DateTimeOffset.Now, UserIsEnabled = true
                };



                var result = await _userManager.CreateAsync(user, NewUser.Password);

                if (result.Succeeded)
                {
                    _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 _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                     *                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
                     */
                    // add CompanyAdmin role to the user

                    await _userManager.AddToRoleAsync(user, "CompanyAdmin");



                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    return(RedirectToPage("/Company/Index"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return(Page());
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }