Ejemplo n.º 1
0
        /// <summary>
        /// Add User
        /// </summary>
        /// <param name="FirstName"></param>
        /// <param name="LastName"></param>
        /// <param name="Email"></param>
        /// <param name="EmpresaId"></param>
        /// <param name="Password"></param>
        public static UserCreateResult AddUser(string UserName, string Password, string FirstName, string LastName, string Email, int EmpresaId)
        {
            try
            {
                var user = new ApplicationUser();
                user.UserName  = UserName;
                user.FirstName = string.IsNullOrEmpty(FirstName) ? UserName : FirstName;
                user.LastName  = string.IsNullOrEmpty(LastName) ? UserName : LastName;
                user.Email     = string.IsNullOrEmpty(Email) ? "*****@*****.**" : Email;
                user.EmpresaID = EmpresaId;

                IdentityManager idManager = new IdentityManager();
                var             chkUser   = idManager.CreateUser(user, Password);
                if (chkUser.Succeeded)
                {
                    SecurityDbContext Db = new SecurityDbContext();
                    var group            = Db.Groups.Where(gr => gr.Name.Equals(Startup.AdminName)).FirstOrDefault();
                    if (group != null)
                    {
                        idManager.AddUserToGroup(user.Id, group.Id);
                    }

                    return(new UserCreateResult()
                    {
                        Succeeded = true
                    });
                }
                else
                {
                    return(new UserCreateResult()
                    {
                        Succeeded = false, Errors = chkUser.Errors.ToList()
                    });
                }
            }
            catch (Exception eX)
            {
                List <string> iErrors = new List <string>();
                iErrors.Add(eX.Message);

                System.Data.Entity.Validation.DbEntityValidationException EntityErrors = eX as System.Data.Entity.Validation.DbEntityValidationException;
                if (EntityErrors != null && EntityErrors.EntityValidationErrors != null && EntityErrors.EntityValidationErrors.Count() > 0)
                {
                    EntityErrors.EntityValidationErrors.ToList().ForEach(err =>
                    {
                        if (!err.IsValid && err.ValidationErrors != null && err.ValidationErrors.Count > 0)
                        {
                            err.ValidationErrors.ToList().ForEach(vE => iErrors.Add($"Propiedad: {vE.PropertyName} - Error: {vE.ErrorMessage}"));
                        }
                    });
                }

                return(new UserCreateResult()
                {
                    Succeeded = false, Errors = iErrors
                });
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Delete Iser
        /// </summary>
        /// <param name="User"></param>
        public static UserDeleteResult ClearUserGroups(ApplicationUser User)
        {
            try
            {
                if (User != null)
                {
                    IdentityManager idManager = new IdentityManager();
                    idManager.ClearGroups(User);

                    return(new UserDeleteResult()
                    {
                        Succeeded = true
                    });
                }
            }
            catch (Exception eX)
            {
                List <string> iErrors = new List <string>();
                iErrors.Add(eX.Message);

                System.Data.Entity.Validation.DbEntityValidationException EntityErrors = eX as System.Data.Entity.Validation.DbEntityValidationException;
                if (EntityErrors != null && EntityErrors.EntityValidationErrors != null && EntityErrors.EntityValidationErrors.Count() > 0)
                {
                    EntityErrors.EntityValidationErrors.ToList().ForEach(err =>
                    {
                        if (!err.IsValid && err.ValidationErrors != null && err.ValidationErrors.Count > 0)
                        {
                            err.ValidationErrors.ToList().ForEach(vE => iErrors.Add($"Propiedad: {vE.PropertyName} - Error: {vE.ErrorMessage}"));
                        }
                    });
                }

                return(new UserDeleteResult()
                {
                    Succeeded = false, Errors = iErrors
                });
            }

            return(new UserDeleteResult()
            {
                Succeeded = true
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Find User
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        internal static ApplicationUser FindUser(string UserName)
        {
            IdentityManager idManager = new IdentityManager();

            return(idManager.GetUser(UserName));
        }