Ejemplo n.º 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var tenant = new Tenant {
                    Name = model.Tenant
                };
                var tenantManager = new Managers.TenantManager();
                var roleName      = IsSystemTenant(tenant.Name) ? "Administrator" : "Manager";
                var exstingTenant = tenantManager.GetTenantByName(model.Tenant);

                if (exstingTenant != null)
                {
                    tenant = exstingTenant;
                }
                else
                {
                    tenant = await tenantManager.SaveTenantAsync(tenant);
                }

                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, Tenant_Id = tenant.Id
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                //This line below will hard code users who register to a role of Manager and can be changed by altering the role below
                await UserManager.AddToRoleAsync(user.Id, roleName);

                if (result.Succeeded)
                {
                    //izenda

                    //determine tenant
                    Tenants izendaTenant = null;
                    if (!IsSystemTenant(tenant.Name))
                    {
                        izendaTenant         = new Tenants();
                        izendaTenant.Active  = true;
                        izendaTenant.Deleted = false;

                        izendaTenant.Name     = tenant.Name;
                        izendaTenant.TenantID = tenant.Name;
                        TenantIntegrationConfig.AddOrUpdateTenant(izendaTenant);
                    }

                    //determine roles
                    var roleDetail = new RoleDetail()
                    {
                        Name             = roleName,
                        TenantUniqueName = tenant.Name,
                        Active           = true,
                        Permission       = new Izenda.BI.Framework.Models.Permissions.Permission(),
                    };

                    var izendaUser = new UserDetail()
                    {
                        UserName        = model.Email,
                        EmailAddress    = model.Email,
                        FirstName       = "John", //todo fix this
                        LastName        = "Doe",
                        TenantDisplayId = izendaTenant?.Name,
                        SystemAdmin     = IsSystemTenant(tenant.Name),
                        Deleted         = false,
                        Active          = true,
                        Roles           = new List <Role>()
                    };

                    izendaUser.Roles.Add(new Role()
                    {
                        Name = roleDetail.Name
                    });

                    RoleIntegrationConfig.AddOrUpdateRole(roleDetail);
                    UserIntegrationConfig.AddOrUpdateUser(izendaUser);

                    user.Tenant = tenant;
                    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));
        }
Ejemplo n.º 2
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            var manager       = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var signInManager = Context.GetOwinContext().Get <ApplicationSignInManager>();

            var tenant = new Tenant()
            {
                Name = Tenant.Text
            };
            var tenantManager = new Managers.TenantManager();
            var exstingTenant = tenantManager.GetTenantByName(tenant.Name);

            if (exstingTenant != null)
            {
                tenant = exstingTenant;
            }
            else
            {
                tenant = tenantManager.SaveTenant(tenant);
            }

            var user = new ApplicationUser()
            {
                UserName = Email.Text, Email = Email.Text, Tenant_Id = tenant.Id
            };
            IdentityResult result = manager.Create(user, Password.Text);

            //This line below will hard code users who register to a role of Manager and can be changed by altering the role below
            manager.AddToRole(user.Id, "Manager");
            if (result.Succeeded)
            {
                //izenda

                //determine tenant
                var izendaTenant = new Tenants();
                izendaTenant.Active  = true;
                izendaTenant.Deleted = false;

                //var currentUserTenant = ParseTenantFromEmail(izendaTenant.Name);
                izendaTenant.Name     = tenant.Name;
                izendaTenant.TenantID = tenant.Name;

                //determine roles
                var roleDetail = new RoleDetail()
                {
                    Name             = "Administrator",
                    TenantUniqueName = tenant.Name,
                    Active           = true,
                    Permission       = new Permission(),
                };

                var izendaUser = new UserDetail()
                {
                    UserName        = user.Email,
                    EmailAddress    = user.Email,
                    FirstName       = "John", //todo fix this
                    LastName        = "Doe",
                    TenantDisplayId = tenant.Name,
                    Deleted         = false,
                    Active          = true,
                    SystemAdmin     = false,
                    Roles           = new List <Role>()
                };

                izendaUser.Roles.Add(new Role()
                {
                    Name = roleDetail.Name
                });

                TenantIntegrationConfig.AddOrUpdateTenant(izendaTenant);
                RoleIntegrationConfig.AddOrUpdateRole(roleDetail);
                UserIntegrationConfig.AddOrUpdateUser(izendaUser);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                //string code = manager.GenerateEmailConfirmationToken(user.Id);
                //string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
                //manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

                signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var tenant = await HandleTenant(Input.Tenant);

                var roleName = IsSystemTenant(tenant.Name) ? "Administrator" : "Manager";

                var user = new IzendaUser {
                    Tenant_Id = tenant.Id, UserName = Input.Email, Email = Input.Email
                };
                var result = await userManager.CreateAsync(user, Input.Password);

                await userManager.AddToRoleAsync(user, roleName);

                if (result.Succeeded)
                {
                    logger.LogInformation("User created a new account with password.");
                    //determine tenant
                    Tenants izendaTenant = null;
                    if (!IsSystemTenant(tenant.Name))
                    {
                        izendaTenant         = new Tenants();
                        izendaTenant.Active  = true;
                        izendaTenant.Deleted = false;

                        izendaTenant.Name     = tenant.Name;
                        izendaTenant.TenantID = tenant.Name;
                        TenantIntegrationConfig.AddOrUpdateTenant(izendaTenant);
                    }

                    //determine roles
                    var roleDetail = new RoleDetail()
                    {
                        Name             = roleName,
                        TenantUniqueName = tenant.Name,
                        Active           = true,
                        Permission       = new Izenda.BI.Framework.Models.Permissions.Permission(),
                    };

                    var izendaUser = new UserDetail()
                    {
                        UserName        = user.Email,
                        EmailAddress    = user.Email,
                        FirstName       = "John", //todo fix this
                        LastName        = "Doe",
                        TenantDisplayId = izendaTenant?.Name,
                        SystemAdmin     = IsSystemTenant(tenant.Name),
                        Deleted         = false,
                        Active          = true,
                        Roles           = new List <Role>()
                    };

                    izendaUser.Roles.Add(new Role()
                    {
                        Name = roleDetail.Name
                    });

                    RoleIntegrationConfig.AddOrUpdateRole(roleDetail);
                    UserIntegrationConfig.AddOrUpdateUser(izendaUser);

                    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());
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var tenant = new Tenant {
                    Name = model.Tenant
                };
                var tenantManager = new Managers.TenantManager();
                var roleName      = IsSystemTenant(tenant.Name) ? "Administrator" : "Manager";
                var exstingTenant = tenantManager.GetTenantByName(model.Tenant);

                if (exstingTenant != null)
                {
                    tenant = exstingTenant;
                }
                else
                {
                    tenant = await tenantManager.SaveTenantAsync(tenant);
                }

                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, Tenant_Id = tenant.Id
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                //This line below will hard code users who register to a role of Manager and can be changed by altering the role below
                await UserManager.AddToRoleAsync(user.Id, roleName);

                if (result.Succeeded)
                {
                    //izenda

                    //determine tenant
                    Tenants izendaTenant = null;
                    if (!IsSystemTenant(tenant.Name))
                    {
                        izendaTenant         = new Tenants();
                        izendaTenant.Active  = true;
                        izendaTenant.Deleted = false;

                        izendaTenant.Name     = tenant.Name;
                        izendaTenant.TenantID = tenant.Name;
                        TenantIntegrationConfig.AddOrUpdateTenant(izendaTenant);
                    }

                    //determine roles
                    var roleDetail = new RoleDetail()
                    {
                        Name             = roleName,
                        TenantUniqueName = tenant.Name,
                        Active           = true,
                        Permission       = new Izenda.BI.Framework.Models.Permissions.Permission(),
                    };

                    var izendaUser = new UserDetail()
                    {
                        UserName        = model.Email,
                        EmailAddress    = model.Email,
                        FirstName       = "John", //todo fix this
                        LastName        = "Doe",
                        TenantDisplayId = izendaTenant?.Name,
                        SystemAdmin     = IsSystemTenant(tenant.Name),
                        Deleted         = false,
                        Active          = true,
                        Roles           = new List <Role>()
                    };

                    izendaUser.Roles.Add(new Role()
                    {
                        Name = roleDetail.Name
                    });

                    RoleIntegrationConfig.AddOrUpdateRole(roleDetail);
                    UserIntegrationConfig.AddOrUpdateUser(izendaUser);

                    user.Tenant = tenant;
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

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

                AddErrors(result);
            }

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