Exemplo n.º 1
0
        public async Task <IActionResult> RegisterIntern(CreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new AppUser
                {
                    Name       = model.Name,
                    Surname    = model.Surname,
                    University = model.University,
                    Major      = model.Major,
                    Department = model.Department,
                    UserName   = model.Email,
                    Email      = model.Email
                };

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

                if (result.Succeeded)
                {
                    UserMgr.AddToRoleAsync(user, "Intern").Wait();
                    return(RedirectToAction("index", "Home"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <User> ChangeToKeyCustomer(User currentUser)
        {
            orderHistory = Context.OrderItems.Include(o => o.Order).Include(ou => ou.Order.User).Where(ou => ou.Order.User == currentUser && ou.Order.Confirmed == true);

            if (orderHistory.Count() > 2)
            {
                await UserMgr.AddToRoleAsync(currentUser, "KeyCustomer");
            }

            return(currentUser);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Register(RegisterBindingModel model)
        {
            try
            {
                User user = await UserMgr.FindByEmailAsync(model.Email);

                if (user == null)
                {
                    user = new User
                    {
                        UserName    = model.UserName,
                        DisplayName = model.DisplayName,
                        Email       = model.Email,
                    };

                    IdentityResult result = await UserMgr.CreateAsync(user, model.Password);

                    await UserMgr.AddToRoleAsync(user, EntityConstants.Role_SuperAdmin);

                    if (result.Succeeded)
                    {
                        var response = new ResponseModel {
                            Status = "Success", Code = "200", Message = "Account Created", Data = model
                        };
                        return(Created("", response));
                    }
                    else
                    {
                        var response = new ResponseModel {
                            Status = "Error", Code = "400", Message = "User registration " + result.ToString(), Data = null
                        };
                        return(Ok(response));
                    }
                }
                else
                {
                    //User Already exsist

                    var response = new ResponseModel {
                        Status = "Error", Code = "400", Message = "Email/UserName already exsist!", Data = null
                    };
                    return(Ok(response));
                }
            }
            catch (Exception ex)
            {
                var response = new ResponseModel {
                    Status = "Error", Code = "400", Message = ex.Message, Data = null
                };
                return(Ok(response));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AdminRegister(RegisterBindingModel model)
        {
            string usertype = User.Claims.First(c => c.Type == "Role").Value;

            if (usertype.Equals(EntityConstants.Role_SuperAdmin) || usertype.Equals(EntityConstants.Role_Admin))
            {
                try
                {
                    User user = await UserMgr.FindByEmailAsync(model.Email);

                    if (user == null)
                    {
                        user = new User
                        {
                            UserName    = model.UserName,
                            DisplayName = model.DisplayName,
                            Email       = model.Email,
                        };

                        IdentityResult result = await UserMgr.CreateAsync(user, model.Password);

                        await UserMgr.AddToRoleAsync(user, model.Role);

                        if (result.Succeeded)
                        {
                            return(Created("", model));
                        }
                        else
                        {
                            var err = new { status = "error", message = "User registration " + result.ToString() };
                            return(BadRequest(err));
                        }
                    }
                    else
                    {
                        //User Already exsist
                        var err = new { status = "error", message = "User already exsist!" };
                        return(BadRequest(err));
                    }
                }
                catch (Exception ex)
                {
                    var err = new { status = "error", message = ex.Message };
                    return(BadRequest(err));
                }
            }
            else
            {
                return(Forbid());
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> SignUp(SignUpForm signUp)
        {
            if (HttpContext.Items["domain"] as string == "default")
            {
                if (!string.IsNullOrEmpty(signUp.TenantDomain) && !string.IsNullOrEmpty(signUp.TenantName) && ModelState.IsValid)
                {
                    if (Regex.Matches(signUp.TenantDomain, @"[a-zA-Z]").Count == signUp.TenantDomain.Length)
                    {
                        var blacklist = new string[] { "default", "admin", "NewTenantUser", "sa", "helper", "dbo", "guest", "sys", "ttask" };
                        if (!_tt.TenantAlreadyExists(signUp.TenantDomain) && !blacklist.Contains(signUp.TenantDomain))
                        {
                            _newTenant.NewTenant(signUp.TenantName, signUp.TenantDomain);
                            Directory.CreateDirectory(@"wwwroot/img/profile/" + signUp.TenantDomain);

                            HttpContext.Items["domain"] = signUp.TenantDomain;

                            int tenantId = _tt.GetTenantId(signUp.TenantDomain);


                            var user = new User()
                            {
                                Id          = 1,
                                UserName    = signUp.Email,
                                Email       = signUp.Email,
                                FirstName   = signUp.FirstName,
                                Surname     = signUp.Surname,
                                PhoneNumber = signUp.PhoneNumber,
                                IdTenant    = tenantId
                            };


                            HttpContext.Items["domain"] = signUp.TenantDomain;
                            IdentityResult result = await UserMgr.CreateAsync(user, signUp.Password);

                            if (result.Succeeded)
                            {
                                await UserMgr.AddToRolesAsync(user, new List <string> {
                                    nameof(Roles.NormalUser), nameof(Roles.DomainAdmin), nameof(Roles.Manager)
                                });

                                var settings = new UserSettings()
                                {
                                    Coloring      = "0",
                                    CustomizeView = "0",
                                    Notifications = "0",
                                    IdUser        = user.Id
                                };
                                _ut.InsertSettings(settings);


                                return(Redirect($"https://{HttpContext.Request.Host}/{signUp.TenantDomain}"));
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("CustomErr", $"Domain {signUp.TenantDomain} already exists.");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("CustomErr", $"Domain has to contain letters only.");
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(signUp.TenantName))
                    {
                        ModelState.AddModelError("signUp.TenantName", "Company name is required field!");
                    }
                    if (string.IsNullOrEmpty(signUp.TenantDomain))
                    {
                        ModelState.AddModelError("signUp.TenantDomain", "Domain is required field!");
                    }
                }
            }
            else
            {
                var idService = _serviceOrderTable.GetInUseServiceIdByTenantId(_tt.GetTenantId(HttpContext.Items["domain"] as string));
                var noUsers   = _ut.GetNOUsers();

                if ((idService == (int)Services.Basic && noUsers < 5) || (idService == (int)Services.Pro && noUsers < 10) || idService == (int)Services.Business)
                {
                    if (ModelState.IsValid)
                    {
                        signUp.TenantDomain = HttpContext.Items["domain"] as string;

                        int tenantId = _tt.GetTenantId(signUp.TenantDomain);
                        int userId   = _ut.GetMaxId();


                        User user = await UserMgr.FindByEmailAsync(signUp.Email);

                        if (user == null)
                        {
                            user = new User()
                            {
                                Id          = userId,
                                UserName    = signUp.Email,
                                Email       = signUp.Email,
                                FirstName   = signUp.FirstName,
                                Surname     = signUp.Surname,
                                PhoneNumber = signUp.PhoneNumber,
                                IdTenant    = tenantId
                            };


                            IdentityResult result = await UserMgr.CreateAsync(user, signUp.Password);

                            if (result.Succeeded)
                            {
                                await UserMgr.AddToRoleAsync(user, nameof(Roles.NormalUser));

                                if (userId == 1)
                                {
                                    await UserMgr.AddToRoleAsync(user, nameof(Roles.DomainAdmin));

                                    await UserMgr.AddToRoleAsync(user, nameof(Roles.Manager));
                                }

                                var settings = new UserSettings()
                                {
                                    Coloring      = "0",
                                    CustomizeView = "0",
                                    Notifications = "0",
                                    IdUser        = user.Id
                                };
                                _ut.InsertSettings(settings);

                                var TenantPolicyResult = await _authorization.AuthorizeAsync(User, "TenantPolicy");

                                if (TenantPolicyResult.Succeeded)
                                {
                                    var msg = "User " + user.FirstName + " " + user.Surname + " has signed up in your application.";
                                    _notificationTable.NotifyUser(1, msg);
                                    var signInResult = await SignInMgr.PasswordSignInAsync(user, signUp.Password, false, false);

                                    if (signInResult.Succeeded)
                                    {
                                        HttpContext.Response.Cookies.Append(
                                            "Identity.Domain",
                                            HttpContext.Items["domain"] as string,
                                            new CookieOptions {
                                            Expires  = DateTime.Now.AddDays(30),
                                            HttpOnly = true,
                                            Secure   = true,
                                            SameSite = SameSiteMode.Lax
                                        }
                                            );
                                        return(RedirectToAction("Index", "HomePage"));
                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError("CustomErr", $"User was created, but service of your domain is not paid. Sign in is impossible. Please contact your domain admin.");
                                }
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("CustomErr", $"User {user.Email} already exists.");
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("CustomErr", $"Number of users has been exceeded! If you want to register, contact domain admin to subscribe to higher service.");
                }
            }

            var model = new IndexViewModel
            {
                Domain         = HttpContext.Items["domain"] as string,
                SignUpSelected = true,
                SignIn         = new SignInForm(),
                SignUp         = signUp
            };

            return(View("Index", model));
        }