过滤数据源,以便用户只能看到自己的信息

时间:2015-02-05 08:29:41

标签: c# sql visual-studio-lightswitch

在我的一个屏幕上,我有一张表格,显示有关客户公司的信息。现在我正在尝试过滤数据源,因此公司X的用户只能查看公司X的信息,而不能查看公司Y或Z的信息。我创建了一个包含aspnet_User指南的表(CustomerUser) ,以及CustomerID,以便公司可以创建多个用户。

这是我到目前为止所拥有的,但它似乎陷入无限循环,因为它抛出了StackOverflowException。

partial void Customers_Filter(ref Expression<Func<Customer, bool>> filter)
    {
        //if (!Application.Current.User.HasPermission(Permissions.SecurityAdministration))
        //{
            //Guid guid = (Guid)Membership.GetUser().ProviderUserKey;                
            Guid guid = new Guid("1657d378-4b8b-ed4e-f928-bb48fc83bf18");

            IEnumerator cusUsers = this.CustomerUsers.GetEnumerator();

            CustomerUser current;
            CustomerUser found = null;
            while (cusUsers.MoveNext())
            {
                current = (CustomerUser)cusUsers.Current;

                if (current.GebruikerID == guid)
                {
                    found = current;
                }
            };

            try
            {                
                if (found != null)
                {
                    filter = e => e.CustomerID == found.Customer1.CustomerID;
                }
                else
                {
                    filter = e => e.CustomerID == "-1";
                }
            }
            catch (Exception ex)
            {

            }

        //}
    }

2 个答案:

答案 0 :(得分:0)

CustomerUsers的枚举器很可能有错误。

但是,如果找到了正确的用户,则会添加break

       while (cusUsers.MoveNext())
       {
            current = (CustomerUser)cusUsers.Current;

            if (current.GebruikerID == guid)
            {
                found = current;
                break;  // stop searching 
            }
        };

       while (found == null && cusUsers.MoveNext())
       {
            current = (CustomerUser)cusUsers.Current;

            if (current.GebruikerID == guid)
            {
                found = current;
            }
        };

答案 1 :(得分:0)

好吧,对于任何有同样问题的人来说,这个问题磕磕绊绊:我添加了一个新的查询并进行了预处理,而不是过滤数据集,这解决了我的问题。像这样:

partial void CustomersByLoggedInUser_PreprocessQuery(ref IQueryable<Customer> query)
    {
    if (!Application.Current.User.HasPermission(Permissions.SecurityAdministration))
    {
        Guid guid = (Guid)Membership.GetUser().ProviderUserKey;                

        IEnumerator cusUsers = this.CustomerUsers.GetEnumerator();

        CustomerUser current;
        CustomerUser found = null;
        while (cusUsers.MoveNext())
        {
            current = (CustomerUser)cusUsers.Current;

            if (current.GebruikerID == guid)
            {
                found = current;
            }
        };

        try
        {                
            if (found != null)
            {
                filter = e => e.CustomerID == found.Customer1.CustomerID;
            }
            else
            {
                filter = e => e.CustomerID == "-1";
            }
        }
        catch (Exception ex)
        {

        }

    }
}