public static MvcHtmlString ActionLinkWithPermission(this HtmlHelper helper, string linkText, string action, string controller, PermissionKeys required)
        {
            if (helper == null ||
                helper.ViewContext == null ||
                helper.ViewContext.RequestContext == null ||
                helper.ViewContext.RequestContext.HttpContext == null ||
                helper.ViewContext.RequestContext.HttpContext.User == null)
                return MvcHtmlString.Empty;

            using (var proxy = new ServiceProxy<IUserService>())
            {
                var role = proxy.Channel.GetUserRoleByUserName(helper.ViewContext.RequestContext.HttpContext.User.Identity.Name);
                if (role == null)
                    return MvcHtmlString.Empty;
                var keyName = ByteartRetailConfigurationReader.Instance.GetKeyNameByRoleName(role.Name);
                if (keyName == null)
                    throw new ConfigurationErrorsException(string.Format("在配置文件中没有定义与角色名称 {0} 相对应的权限键(Permission Key)名称。", role.Name));
                var namesInEnum = Enum.GetNames(typeof(PermissionKeys));
                if (!namesInEnum.Contains(keyName))
                    throw new ConfigurationErrorsException(string.Format("在配置文件中对角色名称 {0} 设定的权限键(Permission Key)名称无效。", role.Name));
                var permissionKey = (PermissionKeys)Enum.Parse(typeof(PermissionKeys), keyName);
                if ((permissionKey & required) == permissionKey)
                    return MvcHtmlString.Create(HtmlHelper.GenerateLink(helper.ViewContext.RequestContext, helper.RouteCollection, linkText, null, action, controller, null, null));
                return MvcHtmlString.Empty;
            }
        }
Пример #2
0
        public static MvcHtmlString ActionLinkWithPermission(this HtmlHelper helper, string linkText, string action, string controller, PermissionKeys required)
        {
            if (helper == null ||
                helper.ViewContext == null ||
                helper.ViewContext.RequestContext == null ||
                helper.ViewContext.RequestContext.HttpContext == null ||
                helper.ViewContext.RequestContext.HttpContext.User == null ||
                helper.ViewContext.RequestContext.HttpContext.User.Identity == null)
                return MvcHtmlString.Empty;
            return MvcHtmlString.Empty;
            //using (var proxy = new UserServiceClient())
            //{
            //    var role = proxy.GetRoleByUserName(helper.ViewContext.RequestContext.HttpContext.User.Identity.Name);
            //    if (role == null)
            //        return MvcHtmlString.Empty;
            //    var keyName = role.Name;
            //    var permissionKey = (PermissionKeys)Enum.Parse(typeof(PermissionKeys), keyName);

            //    // 通过用户的角色和对应对应的权限进行与操作
            //    // 与结果等于用户角色时,表示用户角色与所需要的权限一样,则创建对应权限的链接
            //    return (permissionKey & required) == permissionKey ?
            //        MvcHtmlString.Create(HtmlHelper.GenerateLink(helper.ViewContext.RequestContext, helper.RouteCollection, linkText, null, action, controller, null, null))
            //        : MvcHtmlString.Empty;
            //}
        }
Пример #3
0
 public static bool UserHasAllPermissionIn(Int64 UserId, params string[] PermissionKeys)
 {
     if (PermissionKeys.Length == 0)
     {
         return(true);
     }
     PermissionKeys = PermissionKeys.GetUniqueArray(); // DB won't count duplicates, we won't either.
     return(new Query(UserPermissionMap.TableSchema)
            .Join(JoinType.InnerJoin, Permission.TableSchema, @"map", new JoinColumnPair(UserPermissionMap.TableSchema, UserPermissionMap.Columns.PermissionId, Permission.Columns.PermissionId))
            .Where(UserPermissionMap.TableSchema.SchemaName, UserPermissionMap.Columns.UserId, WhereComparision.EqualsTo, UserId)
            .AND(@"map", Permission.Columns.Key, WhereComparision.In, PermissionKeys)
            .GetCount(UserPermissionMap.TableSchema.SchemaName, UserPermissionMap.Columns.PermissionId) == PermissionKeys.Length);
 }
Пример #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddMvc(option =>
            {
                option.ModelBinderProviders.Insert(0, new WidgetModelBinderProvider());
                option.ModelMetadataDetailsProviders.Add(new DataAnnotationsMetadataProvider());
            })
            .AddControllersAsServices()
            .AddJsonOptions(option =>
            {
                option.SerializerSettings.DateFormatString = "yyyy-MM-dd";
            });
            services.TryAddTransient <IOnDatabaseConfiguring, EntityFrameWorkConfigure>();

            services.UseEasyFrameWork(Configuration, HostingEnvironment).LoadEnablePlugins(plugin =>
            {
                var cmsPlugin = plugin as PluginBase;
                if (cmsPlugin != null)
                {
                    cmsPlugin.InitPlug();
                }
            }, null, () => services);
            services.UseZKEACMS(Configuration);

            services.Configure <AuthorizationOptions>(options =>
            {
                PermissionKeys.Configure(options);
                KnownRequirements.Configure(options);
            });

            //services.AddAuthorization();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
            {
                o.LoginPath        = new PathString("/Account/Login");
                o.AccessDeniedPath = new PathString("/Error/Forbidden");
            })
            .AddCookie(CustomerAuthorizeAttribute.CustomerAuthenticationScheme, option =>
            {
                option.LoginPath = new PathString("/Account/Signin");
            });


            new ResourceManager().Excute();
        }
Пример #5
0
        public static MvcHtmlString ActionLinkWithPermission(this HtmlHelper helper, string linkText, string action, string controller, PermissionKeys required)
        {
            if (helper == null ||
                helper.ViewContext == null ||
                helper.ViewContext.RequestContext == null ||
                helper.ViewContext.RequestContext.HttpContext == null ||
                helper.ViewContext.RequestContext.HttpContext.User == null ||
                helper.ViewContext.RequestContext.HttpContext.User.Identity == null)
            {
                return(MvcHtmlString.Empty);
            }

            using (var proxy = new UserServiceClient())
            {
                var role = proxy.GetRoleByUserName(helper.ViewContext.HttpContext.User.Identity.Name);
                if (role == null)
                {
                    return(MvcHtmlString.Empty);
                }
                var keyName       = role.Name;
                var permissionKey = (PermissionKeys)Enum.Parse(typeof(PermissionKeys), keyName);

                // 通过用户的角色和对应对应的权限进行与操作
                // 与结果等于用户角色时,表示用户角色与所需要的权限一样,则创建对应权限的链接
                //permissionKey & required 按位与运算 (required 权限按位或结果,比如权限 1或2 = 0011 )
                return((permissionKey & required) == permissionKey?
                       MvcHtmlString.Create(HtmlHelper.GenerateLink(helper.ViewContext.RequestContext, helper.RouteCollection,
                                                                    linkText, null, action, controller, null, null)) : MvcHtmlString.Empty);
            }
        }
Пример #6
0
        public static MvcHtmlString ActionLinkWithPermission(this HtmlHelper helper, string linkText, string action, string controller, PermissionKeys required)
        {
            if (helper == null ||
                helper.ViewContext == null ||
                helper.ViewContext.RequestContext == null ||
                helper.ViewContext.RequestContext.HttpContext == null ||
                helper.ViewContext.RequestContext.HttpContext.User == null ||
                helper.ViewContext.RequestContext.HttpContext.User.Identity == null)
            {
                return(MvcHtmlString.Empty);
            }

            using (ServiceProxy <IUserService> proxy = new ServiceProxy <IUserService>())
            {
                var role = proxy.Channel.GetUserRoleByUserName(helper.ViewContext.RequestContext.HttpContext.User.Identity.Name);
                if (role == null)
                {
                    return(MvcHtmlString.Empty);
                }
                var keyName = ByteartRetailConfigurationReader.Instance.GetKeyNameByRoleName(role.Name);
                if (keyName == null)
                {
                    throw new ConfigurationErrorsException(string.Format("在配置文件中没有定义与角色名称 {0} 相对应的权限键(Permission Key)名称。", role.Name));
                }
                var namesInEnum = Enum.GetNames(typeof(PermissionKeys));
                if (!namesInEnum.Contains(keyName))
                {
                    throw new ConfigurationErrorsException(string.Format("在配置文件中对角色名称 {0} 设定的权限键(Permission Key)名称无效。", role.Name));
                }
                var permissionKey = (PermissionKeys)Enum.Parse(typeof(PermissionKeys), keyName);
                if ((permissionKey & required) == permissionKey)
                {
                    return(MvcHtmlString.Create(HtmlHelper.GenerateLink(helper.ViewContext.RequestContext, helper.RouteCollection, linkText, null, action, controller, null, null)));
                }
                else
                {
                    return(MvcHtmlString.Empty);
                }
            }
        }
Пример #7
0
        public static MvcHtmlString ActionLinkWithPermission(this HtmlHelper helper, string linkText, string action, string controller, PermissionKeys required)
        {
            if (helper == null ||
                helper.ViewContext == null ||
                helper.ViewContext.RequestContext == null ||
                helper.ViewContext.RequestContext.HttpContext == null ||
                helper.ViewContext.RequestContext.HttpContext.User == null ||
                helper.ViewContext.RequestContext.HttpContext.User.Identity == null)
            {
                return(MvcHtmlString.Empty);
            }

            var role = RF.Service <IUserService>().GetUserRoleByUserName(helper.ViewContext.RequestContext.HttpContext.User.Identity.Name);

            if (role == null)
            {
                return(MvcHtmlString.Empty);
            }

            var permissionKey = (PermissionKeys)Enum.Parse(typeof(PermissionKeys), role.Name);

            if ((permissionKey & required) == permissionKey)
            {
                return(MvcHtmlString.Create(HtmlHelper.GenerateLink(helper.ViewContext.RequestContext, helper.RouteCollection, linkText, null, action, controller, null, null)));
            }
            else
            {
                return(MvcHtmlString.Empty);
            }
        }