Ejemplo n.º 1
0
        public BlockDataMenuLeftModel GetListMenus(int userId, bool isAdmin)
        {
            BlockDataMenuLeftModel output = new BlockDataMenuLeftModel();


            try
            {
                EntityUserModel entityUser = new EntityUserModel();

                entityUser.UserId  = userId;
                entityUser.IsAdmin = isAdmin;

                output = this.GetListMenus(entityUser);
            }
            catch (Exception ex)
            {
                this.ERROR = ex.ToString();
                this.DBConnection.CloseDBConnect();
            }

            return(output);
        }
Ejemplo n.º 2
0
        public BlockDataMenuLeftModel GetListMenus(EntityUserModel entityUser)
        {
            BlockDataMenuLeftModel output = new BlockDataMenuLeftModel();

            this.DBConnection.OpenDBConnect();
            output.EntityUserModel = entityUser;
            try
            {
                //STEP1: GET ALLOWED MENUID FOR THE CURRENT USER
                string sqlUserMenuIds = " ", sqlRoleMenuIds = "", sqlMenus = "";



                //sqlUserMenuIds = "select distinct * from UserMenu where userid=@userid";
                sqlUserMenuIds = "select MenuId from UserMenu where userid=@userid";
                this.DBConnection.command.Parameters.Clear();
                this.DBConnection.command.CommandText = sqlUserMenuIds;
                this.DBConnection.command.Parameters.AddWithValue("@userid", entityUser.UserId);
                //entityUser.LstSelectedMenu.Clear();
                using (SqlDataReader reader = DBConnection.command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            int menuid = reader.GetInt32(reader.GetOrdinal("MenuId"));
                            if (!entityUser.LstSelectedMenu.Contains(menuid))
                            {
                                entityUser.LstSelectedMenu.Add(menuid);
                            }
                        }
                    }
                }

                //GET MENUID FROM ROLEMENU

                sqlRoleMenuIds = "select distinct rm.MenuId from Users as u, UserRole as ur, RoleMenu as rm where ( " +
                                 "   u.UserId = @userid and u.UserId = ur.UserId and rm.RoleId = ur.RoleId )";

                this.DBConnection.command.CommandText = sqlRoleMenuIds;

                using (SqlDataReader reader = DBConnection.command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            int menuid = reader.GetInt32(reader.GetOrdinal("MenuId"));
                            if (!entityUser.LstSelectedMenu.Contains(menuid))
                            {
                                entityUser.LstSelectedMenu.Add(menuid);
                            }
                        }
                    }
                }
                //STEP2: GET ALL MENU ******************************************************
                sqlMenus = "select * from Menu order by leveltree ";
                this.DBConnection.command.Parameters.Clear();
                this.DBConnection.command.CommandText = sqlMenus;
                List <EntityMenuModel> lstMenuTemp = new List <EntityMenuModel>();
                using (SqlDataReader reader = DBConnection.command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            EntityMenuModel entityMenuModel = new EntityMenuModel();
                            entityMenuModel.FilterCommand = reader.IsDBNull(reader.GetOrdinal("FilterCommand")) ? "" : reader.GetString(reader.GetOrdinal("FilterCommand"));
                            entityMenuModel.FilterValue   = reader.IsDBNull(reader.GetOrdinal("FilterValue")) ? "" : reader.GetString(reader.GetOrdinal("FilterValue"));
                            entityMenuModel.LevelTree     = reader.IsDBNull(reader.GetOrdinal("LevelTree")) ? "" : reader.GetString(reader.GetOrdinal("LevelTree"));
                            entityMenuModel.MenuId        = reader.IsDBNull(reader.GetOrdinal("MenuId")) ? 0 : reader.GetInt32(reader.GetOrdinal("MenuId"));
                            entityMenuModel.MenuLevel     = reader.IsDBNull(reader.GetOrdinal("MenuLevel")) ? "" : reader.GetString(reader.GetOrdinal("MenuLevel"));
                            entityMenuModel.Name          = reader.IsDBNull(reader.GetOrdinal("Name")) ? "" : reader.GetString(reader.GetOrdinal("Name"));
                            entityMenuModel.ParentId      = reader.IsDBNull(reader.GetOrdinal("Name")) ? 0 : reader.GetInt32(reader.GetOrdinal("ParentId"));
                            entityMenuModel.Path          = reader.IsDBNull(reader.GetOrdinal("Path")) ? "" : reader.GetString(reader.GetOrdinal("Path"));
                            entityMenuModel.Status        = reader.IsDBNull(reader.GetOrdinal("Status")) ? false : reader.GetBoolean(reader.GetOrdinal("Status"));

                            lstMenuTemp.Add(entityMenuModel);
                            if (entityUser.IsAdmin == true)
                            {
                                entityUser.LstSelectedMenu.Add(entityMenuModel.MenuId);
                                output.StrAllowedLeveltrees += entityMenuModel.LevelTree + ",";
                                output.StrAllowedMenuIds    += entityMenuModel.MenuId + ",";
                                continue;
                            }
                            if (entityUser.LstSelectedMenu.Contains(entityMenuModel.MenuId))
                            {
                                // output.LstAllowedMenus.Add(entityMenuModel.Clone());
                                output.StrAllowedLeveltrees += entityMenuModel.LevelTree + ",";
                                output.StrAllowedMenuIds    += entityMenuModel.MenuId + ",";
                            }
                        }
                    }
                }
                output.StrAllowedMenuIds    = "," + output.StrAllowedMenuIds;
                output.StrAllowedLeveltrees = "," + output.StrAllowedLeveltrees;
                foreach (EntityMenuModel entity in lstMenuTemp)

                {
                    if (entityUser.LstSelectedMenu.Contains(entity.MenuId))
                    {
                        //output.LstAllowedMenus.Add(entity.Clone());
                        //output.LstAllOfMenus.Add(entity.Clone());
                        continue;
                    }

                    //else
                    //{
                    //    string currentMenuLeveltree = entity.LevelTree + "@@@";
                    //    bool added = false;
                    //    while (currentMenuLeveltree.Length > 0)
                    //    {
                    //        int pos = currentMenuLeveltree.LastIndexOf("@@@");
                    //        if (pos < 0) break;
                    //        currentMenuLeveltree = currentMenuLeveltree.Substring(0, pos);

                    //        //DAY LA TRUONG HOP CO MENU CHA NAM TRONG SO DUOC PHAN QUYEN
                    //        //THI ADD MENU HIEN TAI VAO NHOM
                    //        if (output.StrAllowedLeveltrees.IndexOf("," + currentMenuLeveltree + ",") >= 0)
                    //        {
                    //            output.StrAllowedLeveltrees += entity.LevelTree + ",";
                    //            output.StrAllowedMenuIds += entity.MenuId.ToString() + ",";
                    //            entityUser.LstSelectedMenu.Add(entity.MenuId);
                    //            added = true;
                    //            break;
                    //        }

                    //    }


                    //}
                }

                foreach (EntityMenuModel entity in lstMenuTemp)
                {
                    if (entityUser.IsAdmin == true)
                    {
                        output.LstAllOfMenus.Add(entity.Clone());
                        continue;
                    }

                    if (entityUser.LstSelectedMenu.Contains(entity.MenuId))
                    {
                        output.LstAllOfMenus.Add(entity.Clone());
                    }

                    else
                    {
                        //KIEM TRA MENU HIEN TAI CO LA MENU CHA CUA 1 TRONG SO CAC MENU DA DUOC ADD KO
                        if (output.StrAllowedLeveltrees.Contains("," + entity.LevelTree + "@"))
                        {
                            output.LstAllOfMenus.Add(entity.Clone());
                        }
                    }
                }

                //STEP3: GET LIST OF ALLOWED MENU FOR THE CURRENT USER
            }
            catch (Exception ex)
            {
                this.ERROR = ex.ToString();
            }
            finally
            {
                this.DBConnection.CloseDBConnect();
            }


            return(output);
        }