|
1 | 1 | #encoding:utf-8 |
2 | | -import importlib |
| 2 | +#关于 继承 变量寻找关系 |
| 3 | +#子类继承父类 若某个方法在父类中 子类执行此方法时 执行某个变量的操作 |
| 4 | +#此变量的寻找 (若此变量在父类中存在) 先从子类中寻找 若子类中没有 再去父类中寻找 |
| 5 | +#变量寻找关系 先从子类中寻找 再去父类中寻找 |
3 | 6 |
|
| 7 | +class Scope: |
| 8 | + allow_api = [] |
| 9 | + allow_module = [] |
| 10 | + forbidden = [] |
4 | 11 |
|
5 | | -class SuperScope: |
6 | | - allow_api = ['v1.super_get_user'] |
| 12 | + def __add__(self, other): |
| 13 | + #寻找allow_api 线路 先从继承此类的 子类当中寻找allow_api |
| 14 | + #若子类当中没有 allow_api 在从当前父类中寻找 |
| 15 | + self.allow_api += other.allow_api |
| 16 | + self.allow_api = list(set(self.allow_api)) |
7 | 17 |
|
8 | | -class UserScope: |
9 | | - allow_api = ['v1.get_user'] |
| 18 | + self.allow_module += other.allow_module |
| 19 | + self.allow_module = list(set(self.allow_module)) |
| 20 | + |
| 21 | + self.forbidden += other.forbidden |
| 22 | + self.forbidden = list(set(self.forbidden)) |
| 23 | + |
| 24 | + return self |
| 25 | + |
| 26 | +class UserScope(Scope): |
| 27 | + forbidden = ['v1.user+super_get_user', |
| 28 | + 'v1.user+super_delete_user'] |
| 29 | + def __init__(self): |
| 30 | + self + AdminScope() |
| 31 | + |
| 32 | +class AdminScope(Scope): |
| 33 | + allow_module = ['v1.user'] |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + pass |
| 37 | + |
| 38 | +class SuperScope(Scope): |
| 39 | + allow_module = ['v1.user'] |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + #运算符重载 直接调用基类的 __add__ |
| 43 | + pass |
| 44 | + |
| 45 | +AdminScope() |
| 46 | +SuperScope() |
10 | 47 |
|
11 | 48 | def is_in_scope(scope, endpoint): |
12 | | - cls = globals()[scope] |
13 | | - print(cls.allow_api) |
14 | | - print(endpoint) |
15 | | - if endpoint in cls().allow_api: |
| 49 | + scope = globals()[scope]() |
| 50 | + splits = endpoint.split('+') |
| 51 | + red_name = splits[0] |
| 52 | + |
| 53 | + if endpoint in scope.forbidden: |
| 54 | + return False |
| 55 | + |
| 56 | + if endpoint in scope.allow_api: |
| 57 | + return True |
| 58 | + |
| 59 | + if red_name in scope.allow_module: |
16 | 60 | return True |
17 | 61 | else: |
18 | 62 | return False |
0 commit comments