过滤数据源,使用户只能看到自己的信息
Filtering data source so a user can only see their own info
在我的一个屏幕上,我有一个 table 显示有关客户公司的信息。现在我正在尝试过滤数据源,以便 X 公司的用户只能查看 X 公司的信息,而不能查看 Y 公司或 Z 公司的信息。我创建了一个 table (CustomerUser),其中包含aspnet_User 的 guid 和 CustomerID,以便公司可以创建多个用户。
这是我目前的情况,但它似乎陷入了无限循环,因为它抛出了 WhosebugException。
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)
{
}
//}
}
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;
}
};
好吧,对于遇到同样问题而遇到这个问题的任何人:我添加了一个新查询并对其进行了预处理,而不是过滤数据集,这解决了我的问题。像这样:
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)
{
}
}
}
在我的一个屏幕上,我有一个 table 显示有关客户公司的信息。现在我正在尝试过滤数据源,以便 X 公司的用户只能查看 X 公司的信息,而不能查看 Y 公司或 Z 公司的信息。我创建了一个 table (CustomerUser),其中包含aspnet_User 的 guid 和 CustomerID,以便公司可以创建多个用户。
这是我目前的情况,但它似乎陷入了无限循环,因为它抛出了 WhosebugException。
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)
{
}
//}
}
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;
}
};
好吧,对于遇到同样问题而遇到这个问题的任何人:我添加了一个新查询并对其进行了预处理,而不是过滤数据集,这解决了我的问题。像这样:
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)
{
}
}
}