【问题标题】:What is the best way to handle items visibility per user?处理每个用户的项目可见性的最佳方法是什么?
【发布时间】:2022-01-23 00:13:03
【问题描述】:

我正在寻找处理每个用户的项目可见性的最佳或好方法。 其实我有以下实体...

public class Post
{
  public Guid Id {get;set;}
  public string Content {get; set;}
  public Guid VisibilityId {get; set;}
  public Visibility Visibility {get; set;}
}
public class Visibility
{
  public Guid Id {get; set;}
  public string Name {get; set;}
}
public class AllowedList
{
  public Guid PostId {get; set;}
  public Guid UserId {get; set;}
}

我为可见性实体添加了以下查找

Public, OnlyMe, Custom

我想在案例中为用户显示帖子项目

  1. “公开”在这种情况下,所有用户都允许显示该帖子。
  2. “自定义”在这种情况下,如果用户存在于 AllowedList 中,那么他们可以显示它。
  3. “OnlyMe”在这种情况下,创建的用户只能显示该帖子。

因此,基于该验收标准,我创建了以下代码。

/*
    1. PublicId => 1,
    2. OnlyMeId => 2,
    3. CustomId => 3
 */

public IQueryable<Post> GetPosts(ApplicationContext context ,ICurrentUser user)
{
   var userId = user.Id;
   var posts = context.Posts.Where(p => p.VisibilityId == 1 || (p.VisibilityId  == 2 && p.CreatedBy == userId) || context.AllowedList.Any(a => a.PostId == p.Id && a.UserId == userId));
   return posts; // return as IQueryble of Posts
}

这是我的解决方案,但我认为这不是针对这种情况的正确或好的解决方案。

【问题讨论】:

  • 当范围变量有意义时,请不要使用丢弃 (_)。在这里它们特别令人困惑,因为您还使用__。那么,你不认为它是正确的,请详细说明。
  • @GertArnold 完成,我编辑了问题。

标签: entity-framework asp.net-core asp.net-web-api entity-framework-core


【解决方案1】:

我认为布尔值IsPublic 应该足以表述所有情况,因为应该始终允许创建者查看帖子。 OnlyMe 将对应于具有空允许列表的 Public == false。如果Public 为真,则允许列表中是否有条目无关紧要。

public bool IsPublic { get; set; }
var posts = context.Posts
   .Where(p => p.IsPublic ||
               p.CreatedBy == userId ||
               context.AllowedList.Any(a => a.PostId == p.Id && a.UserId == userId));

【讨论】:

  • 是的,应该始终允许创建者查看帖子。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 2014-05-11
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 2017-06-26
相关资源
最近更新 更多