Ejemplo n.º 1
0
        public bool UpdateUser(Models.Account.User user, bool updateRole = false)
        {
            bool outcome = false;

            if (string.IsNullOrEmpty(user.FirstName) || string.IsNullOrEmpty(user.LastName)) return outcome;

            try
            {
                using (var ctx = new AssessmentEntities())
                {
                    var userObj = ctx.User.Where(x => x.Id == user.Id).SingleOrDefault();

                    if (userObj != null)
                    {
                        userObj.FirstName = user.FirstName;
                        userObj.LastName = user.LastName;
                        userObj.DateUpdated = DateTime.Now;

                        if (updateRole)
                        {
                            var userRoleObj = userObj.UserRole;
                            var roleAdminObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Admin"));
                            var roleMasterObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Master"));

                            if (roleAdminObj != null)
                            {
                                if (user.IsAdmin)
                                {
                                    if (!userRoleObj.Any(x => x.RoleId == roleAdminObj.Id))
                                    {
                                        var newUserRoleObj =
                                            new UserRole()
                                            {
                                                UserId = userObj.Id,
                                                RoleId = roleAdminObj.Id
                                            };

                                        ctx.UserRole.Add(newUserRoleObj);
                                    }
                                    else
                                    {
                                        foreach (var role in userRoleObj.Where(x => x.RoleId == roleAdminObj.Id)) role.Disabled = false;
                                    }
                                }
                                else
                                {
                                    foreach (var role in userRoleObj.Where(x => x.RoleId == roleAdminObj.Id)) role.Disabled = true;
                                }
                            }
                            else
                            {
                                foreach (var item in userRoleObj) item.Disabled = true;
                            }

                            if (roleMasterObj != null)
                            {
                                if (user.IsMaster)
                                {
                                    if (!userRoleObj.Any(x => x.RoleId == roleMasterObj.Id))
                                    {
                                        var newUserRoleObj =
                                            new UserRole()
                                            {
                                                UserId = userObj.Id,
                                                RoleId = roleMasterObj.Id
                                            };

                                        ctx.UserRole.Add(newUserRoleObj);
                                    }
                                    else
                                    {
                                        foreach (var role in userRoleObj.Where(x => x.RoleId == roleMasterObj.Id)) role.Disabled = false;
                                    }
                                }
                                else
                                {
                                    foreach (var role in userRoleObj.Where(x => x.RoleId == roleMasterObj.Id)) role.Disabled = true;
                                }
                            }
                            else
                            {
                                foreach (var item in userRoleObj) item.Disabled = true;
                            }
                        } // end updateRole

                        ctx.SaveChanges();
                        outcome = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in  UpdateUser [user id = " + user.Id + "]: user not found");
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Log(LogLevel.Error, "Error in UpdateUser" + (user != null ? " [user id = " + user.Id + "]: " : ": ") + e.Message);
                throw new Exception();
            }

            return outcome;
        }
Ejemplo n.º 2
0
        public bool AddUser(Models.Account.User user)
        {
            bool outcome = false;
            var now = DateTime.Now;

            if (user != null && string.IsNullOrEmpty(user.Email))
            {
                Logger.Log(LogLevel.Error, "Error in AddUser: email not provided");
                return outcome;
            }

            try
            {
                using (var ctx = new AssessmentEntities())
                {
                    if (ctx.User.Any(x => x.Email == user.Email || x.UserName == user.Email))
                    {
                        Logger.Log(LogLevel.Error, "Error in AddUser [email = " + user.Email + "]: user already exists");
                        return outcome;   // duplicate user
                    }

                    string password_hash = ctx.usp_GetHash(user.Password).SingleOrDefault<string>();

                    // add user
                    var userObj = new User()
                    {
                        Guid = user.Guid,
                        UserName = user.Email,
                        Password = password_hash,
                        Email = user.Email,
                        FirstName = user.FirstName,
                        LastName = user.LastName,
                        StatusId = (int)Models.Account.UserStatus.Unconfirmed,
                        StartDate = now,
                        DateCreated = now,
                        DateUpdated = now
                    };

                    ctx.User.Add(userObj);
                    ctx.SaveChanges();

                    // add Guest role
                    var roleGuestObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Guest"));

                    if (roleGuestObj != null)
                    {
                        var userRoleObj = new UserRole()
                        {
                            UserId = userObj.Id,
                            RoleId = roleGuestObj.Id
                        };

                        ctx.UserRole.Add(userRoleObj);
                    }

                    // add Admin role
                    if (user.IsAdmin)
                    {
                        var roleAdminObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Admin"));

                        if (roleAdminObj != null)
                        {
                            var userRoleObj = new UserRole()
                            {
                                UserId = userObj.Id,
                                RoleId = roleAdminObj.Id
                            };

                            ctx.UserRole.Add(userRoleObj);
                        }
                    }

                    ctx.SaveChanges();
                    outcome = true;
                }
            }
            catch (Exception e)
            {
                Logger.Log(LogLevel.Error, "Error in AddUser" + (user != null ? " [email = " + user.Email + "]: " : ": ") + e.Message);
                throw new Exception();
            }

            return outcome;
        }