public int addCompany(CompanyViewModel c)
        {
            string       uniqueFileName = util.ProcessPhotoproperty(c.Photo);
            CompanyModel model          = new CompanyModel()
            {
                Com_name    = c.Com_name,
                com_country = c.com_country,
                com_Logo    = uniqueFileName,
                isActive    = true
            };

            context.CompanyModel.Add(model);
            context.SaveChanges();
            return(model.Phoneid);
        }
        public async Task <IActionResult> AddEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)

            {
                var user = new ApplicationUser
                {
                    FullName     = model.FullName,
                    UserName     = model.Email,
                    Email        = model.Email,
                    City         = model.cityId,
                    PhoneNumber  = model.PhoneNumber,
                    StreetAdress = model.StreetAdress,
                    store_id     = model.store_id,
                    isactive     = true,
                    Photopath    = util.ProcessPhotoproperty(model.Photo)
                };

                var LoginUser = await Usermanager.GetUserAsync(User);

                user.addedBy = LoginUser.Id;


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

                if (result.Succeeded)
                {
                    foreach (var role in model.Roles.Where(x => x.isSelected))
                    {
                        if (User.IsInRole("Admin") && role.RoleName == "Admin" && role.RoleName == "Super Admin")
                        {
                            await Usermanager.DeleteAsync(user);

                            return(Forbid());
                        }
                    }


                    var roles = await Usermanager.AddToRolesAsync(user, model.Roles.Where(x => x.isSelected).Select(y => y.RoleName));

                    if (!roles.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add selected roles to user! Try Again");
                        await Usermanager.DeleteAsync(user);

                        return(View(model));
                    }
                    // Add all the claims that are selected on the UI
                    var claims = await Usermanager.AddClaimsAsync(user,
                                                                  model.Claims.Select(c => new Claim(c.ClaimType, c.isSelected ? "true" : "false")));

                    if (!claims.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add selected claims to user!  Edit User and Insert Claims from there");
                        return(View(model));
                    }
                    var token = await Usermanager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Email_Confirmation.cshtml", confirmationLink);

                    await _emailSender.SendEmailAsync(user.Email, "Email Confirmation", str);

                    //util.sendemail(user.Email, "Email Confirmation", $"<h2>Here is the Confirmation Link</h2></br> <a href={confirmationLink}>{confirmationLink}</a>");
                    if (Signinmanager.IsSignedIn(User) && (User.IsInRole("Admin") || User.IsInRole("Super Admin")))
                    {
                        return(RedirectToAction("ListUsers", "Administration"));
                    }
                    ViewBag.PageTitle = "Email Confirmation";
                    ViewBag.Title     = "Registration successful";
                    ViewBag.Message   = "Before you can Login, please confirm your " +
                                        "email, by clicking on the confirmation link we have emailed you";
                    return(View("EmailConfirmation"));

                    // await Signinmanager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("Index", "Home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                ViewBag.Stores = util.GetAllStores();
                ViewBag.cities = util.getCities();
                return(View(model));
            }
            ViewBag.cities = util.getCities();
            ViewBag.Stores = util.GetAllStores();
            return(View(model));
        }
Beispiel #3
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var LoginUser = await Usermanager.GetUserAsync(User);

                var user = new ApplicationUser
                {
                    FullName     = model.FullName,
                    UserName     = model.Email,
                    Email        = model.Email,
                    City         = model.cityId,
                    PhoneNumber  = model.PhoneNumber,
                    StreetAdress = model.StreetAdress,
                    Photopath    = util.ProcessPhotoproperty(model.Photo),
                    isactive     = true
                };
                if (Signinmanager.IsSignedIn(User) && User.IsInRole("Employee"))
                {
                    if (!User.HasClaim(claim => claim.Type == "Create User" && claim.Value == "true"))
                    {
                        return(Forbid());
                    }
                    user.store_id = LoginUser.store_id;
                    user.addedBy  = LoginUser.Id;
                }

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

                var roles = await Usermanager.AddToRoleAsync(user, "User");


                if (result.Succeeded)
                {
                    var token = await Usermanager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Email_Confirmation.cshtml", confirmationLink);

                    await _emailSender.SendEmailAsync(user.Email, "Email Confirmation", str);

                    ViewBag.PageTitle = "Email Confirmation";
                    ViewBag.Title     = "Registration successful";
                    ViewBag.Message   = "Before you can Login, please confirm your " +
                                        "email, by clicking on the confirmation link we have emailed you";
                    return(View("EmailConfirmation"));

                    // await Signinmanager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("Index", "Home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                ViewBag.Stores = util.GetAllStores();
                ViewBag.cities = util.getCities();
                return(View(model));
            }
            ViewBag.Stores = util.GetAllStores();
            ViewBag.cities = util.getCities();
            return(View(model));
        }