根据用户类型来控制项目可见范围

  • Post author:
  • Post category:其他


本篇要分享的是对项目的用户类型登录软件的时候控制他们每个人的可见区域也能称之为权限管理。首先我在布局的时候加上Tag(这里我用的是按钮)给它设置的对象值就是数据库对应的模块id,这样是为了之后能分辨它是哪个模块然后根据所登录的用户类型给它进行按钮的显示和隐藏。
  <controls:DMButton BorderThickness="0" Background="#0190f6" Height="40" Margin="0,1,0,0" Tag="1">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding CuisineebroadCommand}" CommandParameter="{Binding ElementName=tab_Main}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <WrapPanel>
                            <Image Height="23" Width="23" Margin="0,0,10,0" Source="/Resources\Images\1 (92).png"/>
                            <Label VerticalAlignment="Center" Content="菜品大类" Foreground="White" />
                        </WrapPanel>
                    </controls:DMButton>
然后给各个模块设置好tag值后绑定命令,我这里是写在页面加载里让它一运行项目就开始根据用户类型显示模块,代码如下:
           //权限菜单
            var varLinq1 = from tbModular in myModels.S_Module
                           join tbJurisdiction in myModels.S_Jurisdiction on tbModular.moduleID equals tbJurisdiction.moduleID
                           join tbUser in myModels.S_User on tbJurisdiction.userID equals tbUser.userID 
                           where tbUser.userID == userId
                           select new
                           {
                               moduleID = tbJurisdiction.moduleID,
                               modulename = tbModular.modulename.Trim(),
                           };

  //左连接
 var userModulars = (from tbModule in myModels.S_Module
                                join tbTemp in varLinq1 on tbModule.moduleID equals tbTemp.moduleID into tempTable
                                select new
                                {
                                    ModularName = tempTable.Select(t => t.modulename).FirstOrDefault() != null ? tempTable.Select(t => t.modulename).FirstOrDefault() : null,
                                    ID = tempTable.Select(t => t.moduleID).FirstOrDefault() != null ? tempTable.Select(t => t.moduleID).FirstOrDefault() : null,
                                    modulename=tbModule.modulename,
                                    moduleID= tbModule.moduleID
                                }).ToList();

           //不同用户登录的模块显示
            for (int i = 0; i < userModulars.Count; i++)
            {
                if (userModulars[i].ID == null)
                {
                    ModalIDs.Add(userModulars[i].moduleID);
                }
            }
            for (int i = 0; i < wp.Count; i++)
            {
                if (ModalIDs.Contains(Convert.ToInt32(wp[i].Tag)))
                {
                    wp[i].Visibility = Visibility.Collapsed;
                }
            }



版权声明:本文为weixin_44546554原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。