Beispiel #1
0
        public async Task <ActionResult> Edit(string userName = null)
        {
            var model = new PuckUserViewModel();

            ViewBag.Level0Type = typeof(PuckUserViewModel);
            if (!string.IsNullOrEmpty(userName))
            {
                var usr = await userManager.FindByNameAsync(userName);

                model.FirstName    = usr.PuckFirstName;
                model.Surname      = usr.PuckSurname;
                model.UserName     = userName;
                model.Email        = usr.Email;
                model.CurrentEmail = usr.Email;
                //model.Password = usr.GetPassword();
                //model.PasswordConfirm = model.Password;
                model.Roles = (await userManager.GetRolesAsync(usr)).ToList();
                if (usr.PuckStartNodeId != Guid.Empty)
                {
                    model.StartNode = new List <PuckPicker> {
                        new PuckPicker {
                            Id = usr.PuckStartNodeId.Value
                        }
                    }
                }
                ;
                model.UserVariant = usr.PuckUserVariant;
            }
            return(View(model));
        }
Beispiel #2
0
        private async Task <List <PuckUserViewModel> > GetUsers()
        {
            var model    = new List <PuckUserViewModel>();
            var puckRole = await roleManager.FindByNameAsync(PuckRoles.Puck);

            var userCollection = repo.GetPuckUser().Where(x => x.Roles.Any(xx => xx.RoleId == puckRole.Id)).ToList();

            foreach (PuckUser pu in userCollection)
            {
                var puvm = new PuckUserViewModel();
                puvm.LastLoginDate       = pu.PuckLastLoginDate;
                puvm.LastLoginDateString = "user has never logged in";
                if (puvm.LastLoginDate.HasValue)
                {
                    puvm.LastLoginDateString = puvm.LastLoginDate.Value.ToString("dd/MM/yyyy hh:mm");
                }
                puvm.UserName  = pu.UserName;
                puvm.Email     = pu.Email;
                puvm.FirstName = pu.PuckFirstName;
                puvm.Surname   = pu.PuckSurname;
                puvm.User      = pu;
                puvm.Roles     = (await userManager.GetRolesAsync(pu)).ToList();
                if (pu.PuckStartNodeId != Guid.Empty)
                {
                    puvm.StartNode = new List <PuckPicker> {
                        new PuckPicker {
                            Id = pu.PuckStartNodeId.Value
                        }
                    }
                }
                ;
                puvm.UserVariant = pu.PuckUserVariant;
                puvm.StartPath   = "/";
                if (pu.PuckStartNodeId != Guid.Empty)
                {
                    var node = repo.GetPuckRevision().FirstOrDefault(x => x.Id == pu.PuckStartNodeId && x.Current);
                    if (node != null)
                    {
                        puvm.StartPath = node.Path;
                    }
                }
                model.Add(puvm);
            }
            return(model);
        }
Beispiel #3
0
        private async Task <List <PuckUserViewModel> > GetUsers()
        {
            var model    = new List <PuckUserViewModel>();
            var puckRole = await roleManager.FindByNameAsync(PuckRoles.Puck);

            var userCollection = repo.GetPuckUser().Where(x => x.Roles.Any(xx => xx.RoleId == puckRole.Id)).ToList();

            foreach (PuckUser pu in userCollection)
            {
                var puvm = new PuckUserViewModel();
                puvm.LastLoginDate       = pu.PuckLastLoginDate;
                puvm.LastLoginDateString = "user has never logged in";
                if (puvm.LastLoginDate.HasValue)
                {
                    puvm.LastLoginDateString = puvm.LastLoginDate.Value.ToString("dd/MM/yyyy hh:mm");
                }
                puvm.UserName  = pu.UserName;
                puvm.Email     = pu.Email;
                puvm.FirstName = pu.PuckFirstName;
                puvm.Surname   = pu.PuckSurname;
                puvm.User      = pu;
                puvm.Roles     = (await userManager.GetRolesAsync(pu)).ToList();
                if (!string.IsNullOrEmpty(pu.PuckStartNodeIds))
                {
                    puvm.StartNodes = pu.PuckStartNodeIds.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => new PuckReference {
                        Id = Guid.Parse(x)
                    }).ToList();
                }
                puvm.UserVariant = pu.PuckUserVariant;
                puvm.StartPaths  = "/";
                if (puvm.StartNodes != null && puvm.StartNodes.Any())
                {
                    Guid[] startIds = puvm.StartNodes.Select(x => x.Id).ToArray();
                    var    nodes    = repo.GetPuckRevision().Where(x => startIds.Contains(x.Id) && x.Current).ToList().GroupBy(x => x.Id);
                    if (nodes.Any())
                    {
                        puvm.StartPaths = string.Join(",", nodes.Select(x => x.First().Path));
                    }
                }
                model.Add(puvm);
            }
            return(model);
        }
Beispiel #4
0
        public async Task <ActionResult> Edit(string userName = null)
        {
            var model = new PuckUserViewModel();

            ViewBag.Level0Type = typeof(PuckUserViewModel);
            if (!string.IsNullOrEmpty(userName))
            {
                var usr = await userManager.FindByNameAsync(userName);

                model.FirstName    = usr.PuckFirstName;
                model.Surname      = usr.PuckSurname;
                model.UserName     = userName;
                model.Email        = usr.Email;
                model.CurrentEmail = usr.Email;
                model.UserGroups   = usr.PuckUserGroups;
                //model.Password = usr.GetPassword();
                //model.PasswordConfirm = model.Password;

                /*var userGroupMetas = repo.GetPuckMeta().Where(x => x.Name == DBNames.UserGroup).ToList()
                 *  .Select(x=>new PuckUserGroupViewModel {
                 *      Id=x.Id,
                 *      Name=x.Key,
                 *      Roles = x.Value?.Split(',',StringSplitOptions.RemoveEmptyEntries)?.ToList()
                 *  }).ToList();
                 * model.CurrentUserGroups = userGroupMetas ?? new List<PuckUserGroupViewModel>();
                 */
                model.Roles = (await userManager.GetRolesAsync(usr)).ToList();
                if (!string.IsNullOrEmpty(usr.PuckStartNodeIds))
                {
                    model.StartNodes = usr.PuckStartNodeIds
                                       .Split(',', StringSplitOptions.RemoveEmptyEntries)
                                       .Select(x => new PuckReference {
                        Id = Guid.Parse(x)
                    })
                                       .ToList();
                }
                model.UserVariant = usr.PuckUserVariant;
            }
            return(View(model));
        }
Beispiel #5
0
        public async Task <JsonResult> Edit(PuckUserViewModel user, bool edit)
        {
            bool   success     = false;
            string message     = "";
            string startPath   = "/";
            Guid   startNodeId = Guid.Empty;
            var    model       = new PuckUserViewModel();

            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("model invalid.");
                }
                if (!edit)
                {
                    if (string.IsNullOrEmpty(user.Password))
                    {
                        throw new Exception("please enter a password");
                    }

                    var puser = new PuckUser
                    {
                        PuckFirstName   = user.FirstName,
                        PuckSurname     = user.Surname,
                        Email           = user.Email,
                        UserName        = user.UserName,
                        PuckUserVariant = user.UserVariant,
                        PuckStartNodeId = user.StartNode?.FirstOrDefault()?.Id ?? Guid.Empty
                    };
                    var result = await userManager.CreateAsync(puser, user.Password);

                    if (!result.Succeeded)
                    {
                        message = string.Join(" ", result.Errors.Select(x => x.Description));
                        throw new Exception(message);
                    }
                    if (user.Roles != null && user.Roles.Count > 0)
                    {
                        await userManager.AddToRolesAsync(puser, user.Roles.ToArray());
                    }
                    if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck))
                    {
                        await userManager.AddToRoleAsync(puser, PuckRoles.Puck);
                    }
                    success = true;
                }
                else
                {
                    var puser = await userManager.FindByEmailAsync(user.CurrentEmail);

                    if (puser == null)
                    {
                        throw new Exception("could not find user for edit");
                    }

                    if (!puser.Email.Equals(user.Email))
                    {
                        puser.Email = user.Email;
                    }
                    if (!puser.UserName.Equals(user.UserName))
                    {
                        puser.UserName = user.UserName;
                    }
                    user.Roles = user.Roles ?? new List <string>();
                    var           roles         = (await userManager.GetRolesAsync(puser)).ToList();
                    List <string> rolesToAdd    = new List <string>();
                    List <string> rolesToRemove = new List <string>();
                    //get roles to remove
                    foreach (var r in roles)
                    {
                        if (!user.Roles.Contains(r))
                        {
                            rolesToRemove.Add(r);
                        }
                    }
                    //get roles to add
                    foreach (var r in user.Roles)
                    {
                        if (!roles.Contains(r))
                        {
                            rolesToAdd.Add(r);
                        }
                    }
                    rolesToRemove.RemoveAll(x => x.Equals(PuckRoles.Puck));
                    if (rolesToRemove.Count > 0)
                    {
                        await userManager.RemoveFromRolesAsync(puser, rolesToRemove.ToArray());
                    }
                    if (rolesToAdd.Count > 0)
                    {
                        await userManager.AddToRolesAsync(puser, rolesToAdd);
                    }

                    if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck))
                    {
                        await userManager.AddToRoleAsync(puser, PuckRoles.Puck);
                    }

                    if (user.StartNode == null || user.StartNode.Count == 0)
                    {
                        puser.PuckStartNodeId = Guid.Empty;
                    }
                    else
                    {
                        Guid picked_id = user.StartNode.First().Id;
                        var  revision  = repo.GetPuckRevision().Where(x => x.Id == picked_id && x.Current).FirstOrDefault();
                        if (revision != null)
                        {
                            startPath = revision.Path + "/";
                        }
                        puser.PuckStartNodeId = picked_id;
                    }
                    if (!string.IsNullOrEmpty(user.UserVariant))
                    {
                        puser.PuckUserVariant = user.UserVariant;
                    }
                    puser.PuckFirstName = user.FirstName;
                    puser.PuckSurname   = user.Surname;
                    await userManager.UpdateAsync(puser);

                    startNodeId = puser.PuckStartNodeId.Value;

                    if (!string.IsNullOrEmpty(user.Password))
                    {
                        var token = await userManager.GeneratePasswordResetTokenAsync(puser);

                        var result = await userManager.ResetPasswordAsync(puser, token, user.Password);

                        if (result.Succeeded)
                        {
                            success = true;
                        }
                        else
                        {
                            success = false;
                            message = string.Join(" ", result.Errors.Select(x => x.Description));
                        }
                    }
                    else
                    {
                        success = true;
                    }
                }
            }
            catch (Exception ex) {
                log.Log(ex);
                success = false;
                message = ex.Message;
            }
            return(Json(new { success = success, message = message, startPath = startPath, startNodeId = startNodeId }));
        }
Beispiel #6
0
        public async Task <JsonResult> Edit(PuckUserViewModel user, bool edit)
        {
            bool   success      = false;
            string message      = "";
            string startPaths   = "";
            string startNodeIds = "";
            var    model        = new PuckUserViewModel();

            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("model invalid.");
                }
                if (string.IsNullOrEmpty(user.UserGroups))
                {
                    throw new Exception("please select at least one user group");
                }

                if (!edit)
                {
                    if (string.IsNullOrEmpty(user.Password))
                    {
                        throw new Exception("please enter a password");
                    }

                    var puser = new PuckUser
                    {
                        PuckFirstName    = user.FirstName,
                        PuckSurname      = user.Surname,
                        Email            = user.Email,
                        UserName         = user.UserName,
                        PuckUserVariant  = user.UserVariant,
                        PuckStartNodeIds = user.StartNodes == null ? null : string.Join(",", user.StartNodes.Select(x => x.Id.ToString())),
                        PuckUserGroups   = user.UserGroups
                    };
                    var result = await userManager.CreateAsync(puser, user.Password);

                    if (!result.Succeeded)
                    {
                        message = string.Join(" ", result.Errors.Select(x => x.Description));
                        throw new Exception(message);
                    }
                    if (user.Roles != null && user.Roles.Count > 0)
                    {
                        await userManager.AddToRolesAsync(puser, user.Roles.ToArray());
                    }
                    if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck))
                    {
                        await userManager.AddToRoleAsync(puser, PuckRoles.Puck);
                    }
                    success = true;
                }
                else
                {
                    var puser = await userManager.FindByEmailAsync(user.CurrentEmail);

                    if (puser == null)
                    {
                        throw new Exception("could not find user for edit");
                    }

                    puser.PuckUserGroups = user.UserGroups;

                    if (!puser.Email.Equals(user.Email))
                    {
                        puser.Email = user.Email;
                    }
                    if (!puser.UserName.Equals(user.UserName))
                    {
                        puser.UserName = user.UserName;
                    }
                    user.Roles = user.Roles ?? new List <string>();
                    var           roles         = (await userManager.GetRolesAsync(puser)).Where(x => x.StartsWith("_")).ToList();
                    List <string> rolesToAdd    = new List <string>();
                    List <string> rolesToRemove = new List <string>();
                    //get roles to remove
                    foreach (var r in roles)
                    {
                        if (!user.Roles.Contains(r))
                        {
                            rolesToRemove.Add(r);
                        }
                    }
                    //get roles to add
                    foreach (var r in user.Roles)
                    {
                        if (!roles.Contains(r))
                        {
                            rolesToAdd.Add(r);
                        }
                    }
                    rolesToRemove.RemoveAll(x => x.Equals(PuckRoles.Puck));
                    if (rolesToRemove.Count > 0)
                    {
                        await userManager.RemoveFromRolesAsync(puser, rolesToRemove.ToArray());
                    }
                    if (rolesToAdd.Count > 0)
                    {
                        await userManager.AddToRolesAsync(puser, rolesToAdd);
                    }

                    if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck))
                    {
                        await userManager.AddToRoleAsync(puser, PuckRoles.Puck);
                    }

                    if (user.StartNodes == null || user.StartNodes.Count == 0)
                    {
                        puser.PuckStartNodeIds = null;
                    }
                    else
                    {
                        Guid[] startIds = user.StartNodes.Select(x => x.Id).ToArray();
                        var    nodes    = repo.GetPuckRevision().Where(x => startIds.Contains(x.Id) && x.Current).ToList().GroupBy(x => x.Id);
                        if (nodes.Any())
                        {
                            startPaths   = string.Join(",", nodes.Select(x => x.First().Path));
                            startNodeIds = string.Join(",", nodes.Select(x => x.First().Id.ToString()));
                        }
                        puser.PuckStartNodeIds = string.Join(",", startIds.Select(x => x.ToString()));
                    }
                    if (!string.IsNullOrEmpty(user.UserVariant))
                    {
                        puser.PuckUserVariant = user.UserVariant;
                    }
                    puser.PuckFirstName = user.FirstName;
                    puser.PuckSurname   = user.Surname;
                    await userManager.UpdateAsync(puser);

                    if (!string.IsNullOrEmpty(user.Password))
                    {
                        var token = await userManager.GeneratePasswordResetTokenAsync(puser);

                        var result = await userManager.ResetPasswordAsync(puser, token, user.Password);

                        if (result.Succeeded)
                        {
                            success = true;
                        }
                        else
                        {
                            success = false;
                            message = string.Join(" ", result.Errors.Select(x => x.Description));
                        }
                    }
                    else
                    {
                        success = true;
                    }
                    cache.Set <bool?>($"renewPuckClaims{user.UserName}", true);
                }
            }
            catch (Exception ex) {
                log.Log(ex);
                success = false;
                message = ex.Message;
            }
            return(Json(new { success = success, message = message, startPaths = startPaths, startNodeIds = startNodeIds }));
        }