|
| 1 | +# --- |
| 2 | +# Absinthe.Middleware behaviour |
| 3 | +# see https://hexdocs.pm/absinthe/Absinthe.Middleware.html#content |
| 4 | +# --- |
| 5 | +# RBAC vs CBAC |
| 6 | +# https://stackoverflow.com/questions/22814023/role-based-access-control-rbac-vs-claims-based-access-control-cbac-in-asp-n |
| 7 | + |
| 8 | +# 本中间件会隐式的加载 community 的 rules 信息,并应用该 rules 信息 |
| 9 | +defmoduleMastaniServerWeb.Middleware.Passportdo |
| 10 | +@behaviourAbsinthe.Middleware |
| 11 | + |
| 12 | +importHelper.Utils |
| 13 | + |
| 14 | +defcall(%{errors:errors}=resolution,_)whenlength(errors)>0,do:resolution |
| 15 | + |
| 16 | +defcall(%{arguments:%{passport_is_owner:true}}=resolution,claim:"owner"),do:resolution |
| 17 | + |
| 18 | +defcall(%{arguments:%{passport_is_owner:true}}=resolution,claim:"owner;"<>_rest), |
| 19 | +do:resolution |
| 20 | + |
| 21 | +defcall( |
| 22 | +%{ |
| 23 | +context:%{cur_user:%{cur_passport:_}}, |
| 24 | +arguments:%{community:_,part:_} |
| 25 | +}=resolution, |
| 26 | +claim:"cms->c?->p?."<>_rest=claim |
| 27 | +)do |
| 28 | +IO.inspect("catch me cms->c?->p?",label:"[passport]") |
| 29 | +resolution|>check_passport_stamp(claim) |
| 30 | +end |
| 31 | + |
| 32 | +defcall( |
| 33 | +%{ |
| 34 | +context:%{cur_user:%{cur_passport:_}}, |
| 35 | +arguments:%{passport_communities:_} |
| 36 | +}=resolution, |
| 37 | +claim:"cms->c?->"<>_rest=claim |
| 38 | +)do |
| 39 | +# IO.inspect("catch me cms->c?->", label: "[passport]") |
| 40 | +resolution|>check_passport_stamp(claim) |
| 41 | +end |
| 42 | + |
| 43 | +defcall( |
| 44 | +%{ |
| 45 | +context:%{cur_user:%{cur_passport:_}}, |
| 46 | +arguments:%{passport_communities:_} |
| 47 | +}=resolution, |
| 48 | +claim:"owner;"<>claim |
| 49 | +)do |
| 50 | +resolution|>check_passport_stamp(claim) |
| 51 | +end |
| 52 | + |
| 53 | +defcall(resolution,_)do |
| 54 | +resolution|>handle_absinthe_error("PassportError: your passport not qualified.") |
| 55 | +end |
| 56 | + |
| 57 | +defpcheck_passport_stamp(resolution,claim)do |
| 58 | +conddo |
| 59 | +claim|>String.starts_with?("cms->c?->p?.")-> |
| 60 | +resolution|>two_step_check(claim) |
| 61 | + |
| 62 | +claim|>String.starts_with?("cms->c?->")-> |
| 63 | +resolution|>one_step_check(claim) |
| 64 | + |
| 65 | +true-> |
| 66 | +resolution|>handle_absinthe_error("PassportError: Passport not qualified.") |
| 67 | +end |
| 68 | +end |
| 69 | + |
| 70 | +defptwo_step_check(resolution,claim)do |
| 71 | +cur_passport=resolution.context.cur_user.cur_passport |
| 72 | +community=resolution.arguments.community |
| 73 | +part=resolution.arguments.part|>to_string |
| 74 | + |
| 75 | +path= |
| 76 | +claim |
| 77 | +|>String.replace("c?",community) |
| 78 | +|>String.replace("p?",part) |
| 79 | +|>String.split("->") |
| 80 | + |
| 81 | +caseget_in(cur_passport,path)do |
| 82 | +true->resolution |
| 83 | +nil->resolution|>handle_absinthe_error("PassportError: Passport not qualified.") |
| 84 | +end |
| 85 | +end |
| 86 | + |
| 87 | +defpone_step_check(resolution,claim)do |
| 88 | +cur_passport=resolution.context.cur_user.cur_passport |
| 89 | +communities=resolution.arguments.passport_communities |
| 90 | + |
| 91 | +result= |
| 92 | +communities |
| 93 | +|>Enum.filter(fncommunity-> |
| 94 | +path=claim|>String.replace("c?",community.title)|>String.split("->") |
| 95 | +get_in(cur_passport,path)==true |
| 96 | +end) |
| 97 | +|>length |
| 98 | + |
| 99 | +caseresult>0do |
| 100 | +true->resolution |
| 101 | +false->resolution|>handle_absinthe_error("PassportError: Passport not qualified.") |
| 102 | +end |
| 103 | +end |
| 104 | +end |
| 105 | + |
| 106 | +# 可以编辑某个社区 post 版块的文章, 支持 owner |
| 107 | +# middleware(M.Passport, claim: "cms->c?->posts.articles.edit") |
| 108 | +# middleware(M.Passport, claim: "owner;cms->c?->posts.articles.edit") |
| 109 | + |
| 110 | +# 可以添加某个社区 posts 版块的 tag 标签, 同时可支持 owner |
| 111 | +# middleware(M.Passport, claim: "cms->c?->posts.tag.add") |
| 112 | +# middleware(M.Passport, claim: "cms->c?->posts.tag.edit") |
| 113 | +# middleware(M.Passport, claim: "cms->c?->posts.tag.delete") |
| 114 | +# middleware(M.Passport, claim: "cms->c?->posts.tag.trash") |
| 115 | +# middleware(M.Passport, claim: "owner;cms->c?->posts.tag.delete") |
| 116 | + |
| 117 | +# 可以给某个社区 posts 版块的 posts 设置标签(setTag), 同时可支持 owner? |
| 118 | +# middleware(M.Passport, claim: "c?->posts.setTag") |
| 119 | + |
| 120 | +# 可以某个社区的 posts 版块置顶 |
| 121 | +# middleware(M.Passport, claim: "cms->c?->posts.setTop") |
| 122 | + |
| 123 | +# 可以编辑某个社区所有版块的文章 |
| 124 | +# middleware(M.Passport, claim: "cms->c?->posts.articles.edit") |
| 125 | +# middleware(M.Passport, claim: "cms->c?->job.articles.edit") |
| 126 | +# ....全部显示声明.... |
| 127 | +# middleware(M.Passport, claim: "cms->c?->radar.articles.edit") |
| 128 | + |
| 129 | +# 可以给某个社区的某个版块添加/删除管理员, 实际上就是在给其他成员分配上面的权限,同时该用户会被添加到相应的管理员中 |
| 130 | +# middleware(M.Passport, claim: "cms->c?->posts.managers.add") |
| 131 | +# middleware(M.Passport, claim: "cms->c?->jobs.managers.add") |
| 132 | +# middleware(M.Passport, claim: "cms->c?->videos.managers.add") |
| 133 | +# middleware(M.Passport, claim: "cms->c?->videos.managers.delete") |
| 134 | + |
| 135 | +# 可以给社区的版块设置审核后发布 |
| 136 | +# middleware(M.Passport, claim: "cms->c?->settings.posts.needReview") |
| 137 | +# middleware(M.Passport, claim: "cms->c?->posts.reviewer") # 审核员 (一开始没必要加) |
| 138 | + |
| 139 | +# 在某个社区的某个版块屏蔽某个用户 |
| 140 | +# middleware(M.Passport, claim: "cms->c?->viewer->block") |
| 141 | + |
| 142 | +# 查看某个社区的总访问量 |
| 143 | +# middleware(M.Passport, claim: "statistics->c?->click") |
| 144 | +# middleware(M.Passport, claim: "logs->c?->posts ...") |
| 145 | + |
| 146 | +# defguard the_fuck(value) when String.contains?(value, "->?") |
| 147 | +# classify the require of this gateway |