Ejemplo n.º 1
0
 //====================================================================================
 public ActionResult link(string userid)
 {
     try
     {
         var Link  = dbcontext.User_LinkTo_UserGroup.Where(m => m.User_ID == userid).ToList();
         var group = dbcontext.User_Group_Info.ToList();
         var listt = new List <SelectListItem>();
         foreach (var team in group)
         {
             if (Link.Any(ma => ma.User_Group_Code == team.User_Group_Code && ma.User_ID == userid))
             {
                 listt.Add(new SelectListItem
                 {
                     Text     = team.User_Group_Code + "---" + team.User_Group_Desc,
                     Value    = team.ID.ToString(),
                     Selected = true
                 });
             }
             else
             {
                 listt.Add(new SelectListItem
                 {
                     Text     = team.User_Group_Code + "---" + team.User_Group_Desc,
                     Value    = team.ID.ToString(),
                     Selected = false
                 });
             }
         }
         var model = new link_ViewModel {
             AvailableFruits = listt, userID = userid
         };
         return(View(model));
     }
     catch (Exception e)
     {
         return(RedirectToAction("index"));
     }
 }
Ejemplo n.º 2
0
        public ActionResult link(link_ViewModel model, FormCollection form)
        {
            var Link  = dbcontext.User_LinkTo_UserGroup.Where(m => m.User_ID == model.userID).ToList();
            var group = dbcontext.User_Group_Info.ToList();
            var listt = new List <SelectListItem>();

            foreach (var team in group)
            {
                if (Link.Any(ma => ma.User_Group_Code == team.User_Group_Code && ma.User_ID == model.userID))
                {
                    listt.Add(new SelectListItem
                    {
                        Text     = team.User_Group_Code + "---" + team.User_Group_Desc,
                        Value    = team.ID.ToString(),
                        Selected = true
                    });
                }
                else
                {
                    listt.Add(new SelectListItem
                    {
                        Text     = team.User_Group_Code + "---" + team.User_Group_Desc,
                        Value    = team.ID.ToString(),
                        Selected = false
                    });
                }
            }
            var model2 = new link_ViewModel {
                AvailableFruits = listt, userID = model.userID
            };

            try
            {
                var user = dbcontext.Users.FirstOrDefault(m => m.Id == model.userID);
                UserManager <ApplicationUser> userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(dbcontext));

                if (true)
                {
                    var Link2 = dbcontext.User_LinkTo_UserGroup.Where(m => m.User_ID == model.userID).ToList();
                    if (Link2.Count() > 0)
                    {
                        dbcontext.User_LinkTo_UserGroup.RemoveRange(Link2);
                        dbcontext.SaveChanges();
                    }
                    //====remove form UserRole table
                    foreach (var item in Link2)
                    {
                        var my_group = dbcontext.User_Group_Info.FirstOrDefault(m => m.User_Group_Code == item.User_Group_Code);
                        var per_link_with_this_group = dbcontext.group_with_permission.Where(m => m.User_Group_InfoID == my_group.ID).ToList();
                        foreach (var per in per_link_with_this_group)
                        {
                            userManager.RemoveFromRole(user.Id, per.Rolle_name);
                        }
                    }
                }
                var link = form["AvailableFruits"].Split(',');
                if (link.Count() > 1)
                {
                    foreach (var item in link)
                    {
                        if (item != "")
                        {
                            var ID     = int.Parse(item);
                            var group2 = dbcontext.User_Group_Info.FirstOrDefault(m => m.ID == ID);
                            dbcontext.User_LinkTo_UserGroup.Add(new User_LinkTo_UserGroup {
                                Created_By = User.Identity.Name, Created_Date = DateTime.Now.Date, User_ID = model.userID, User_Group_Code = group2.User_Group_Code
                            });
                            dbcontext.SaveChanges();
                            ///add new permission  to UserRole table
                            var my_group = dbcontext.User_Group_Info.FirstOrDefault(m => m.User_Group_Code == group2.User_Group_Code);
                            var per_link_with_this_group = dbcontext.group_with_permission.Where(m => m.User_Group_InfoID == my_group.ID).ToList();
                            foreach (var per in per_link_with_this_group)
                            {
                                userManager.AddToRole(user.Id, per.Rolle_name);
                            }
                            ///
                        }
                    }
                }
                return(RedirectToAction("edit_profile", "account", new { id = model.userID }));
            }
            catch (Exception)
            {
                return(View(model2));
            }
        }