Ejemplo n.º 1
0
        private bool CreateCustomUser(CreateUserBindingModel model)
        {
            string hash, stamp;

            CreateHashNStamp(model.UserID, string.IsNullOrWhiteSpace(model.Password) ? DefaultPassword : model.Password, out hash, out stamp);

            AspNetUser user = new AspNetUser()
            {
                Id            = Guid.NewGuid().ToString(),
                UserName      = model.UserID,
                Email         = model.UserID,
                PasswordHash  = hash,
                SecurityStamp = stamp
            };
            Tenant tn = TenantHandler.GetTenantByName(model.Tenant);

            if (tn != null)
            {
                user.TenantId = tn.Id;
            }

            dbCtx.AspNetUsers.Add(user);
            int savedNum = dbCtx.SaveChanges();

            return(savedNum > 0 && user != null);
        }
Ejemplo n.º 2
0
        public string CreateUser(CreateUserBindingModel model)
        {
            string ret = "";

            try
            {
                Tenant tn = null;
                if (!string.IsNullOrEmpty(model.Tenant))
                {
                    tn = TenantHandler.GetTenantByName(model.Tenant);
                }
                if (dbCtx.AspNetUsers.Any(u => (u.UserName.ToLower().Equals(model.UserID.ToLower())) &&
                                          (tn == null ? u.TenantId == null : u.TenantId == tn.Id)))
                {
                    throw new AppException("The user ID: \"" + model.UserID + "\" conflicts with the name in our DB" + (tn == null ? "":(" for tenant " + tn.Name)));
                }
                bool isSaved = CreateCustomUser(model);
                if (isSaved)
                {
                    isSaved = CreateIzendaUser(model);
                }
                if (!isSaved)
                {
                    ret = "Create user failed.";
                }
            }
            catch (AppException ex)
            {
                ret = ex.Message;
            }
            return(ret);
        }
Ejemplo n.º 3
0
        private bool CreateIzendaUser(CreateUserBindingModel model)
        {
            bool ret = false;

            try
            {
                //check if the tenant name provided
                if (!string.IsNullOrWhiteSpace(model.Tenant))
                {
                    //check if the tenant exists / create new if not
                    Tenant tn = TenantHandler.GetTenantByName(model.Tenant);
                    if (tn == null)
                    {
                        CreateTenantBindingModel tm = new CreateTenantBindingModel()
                        {
                            TenantName = model.Tenant, TenantId = model.Tenant
                        };
                        TenantHandler th = new TenantHandler();
                        if (!string.IsNullOrEmpty(th.CreateTenant(tm)))
                        {
                            return(false);
                        }
                    }
                }
                string adminToken = IzendaTokenAuthorization.GetIzendaAdminToken();

                string      assignedRole = String.IsNullOrEmpty(model.SelectedRole) ? "Employee" : model.SelectedRole;
                Task <bool> createdUser  = IzendaUtilities.CreateIzendaUser(
                    model.Tenant,
                    model.UserID,
                    model.LastName,
                    model.FirstName,
                    model.IsAdmin,
                    assignedRole,
                    adminToken);
                // launch the task async and wait for the result.
                ret = createdUser.Result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(ret);
        }