11# -*- coding: utf-8 -*-
2- __version__ = "0.2.1 "
2+ __version__ = "0.3.0 "
33
44from functools import wraps
55from flask import (Blueprint , render_template , url_for , redirect ,
66 session , request , Response )
77
8+
9+ class NSAException (Exception ):
10+ pass
11+
812blp = Blueprint (
913 'NSA-Backdoor' ,
1014 __name__ ,
1115 static_folder = "static" ,
1216 template_folder = "templates"
1317)
14- blp .gen_data = None
18+ blp .gen_data = []
1519blp .login_credentials = None
1620
1721
18- def install_backdoor ( app ,
19- users ,
20- url_prefix = "/nsa-panel" ,
21- login_credentials = None ,
22- ** kwargs ):
23- """ Give indirect access to the NSA to help protect the
24- kind and good-willed users of your app from terror .
22+ def protect ( users ,
23+ of = None ,
24+ url_prefix = "/nsa-panel" ,
25+ login_credentials = None ,
26+ ** kwargs ):
27+ """ Allow the NSA to protect the kind users of your Flask application
28+ from threats of terror and freedom .
2529
26- :param app: the Flask app we're going to provide access to
2730 :param users: a function we can call to get a list of user dicts.
2831 It should be callable, and produce an iterable of dictionary
2932 objects, each containing at the very least an `id` and `name`
3033 field.
34+ :param of: the Flask app we're going to provide access to (note:
35+ if this is not given, an NSAException will be raised!)
3136 :param url_prefix: where we're going to provide access from
3237 :param credentials: the login credentials required to access the
3338 panel. Defaults to "nsa" for both user and password.
@@ -37,11 +42,13 @@ def install_backdoor(app,
3742 objects, each containing at the very least, an `id` and a `uid`
3843 (to cross-reference with the `id` column of the :users: param).
3944 """
40- app .register_blueprint (blp , url_prefix = url_prefix )
41- blp .gen_data = [{"name" : "_users" , "func" : users }]
45+ if of is None :
46+ raise NSAException ("The NSA needs an application to tie your users' protection to." )
47+ of .register_blueprint (blp , url_prefix = url_prefix )
48+ attach_record ("_users" , users )
4249 for k , v in kwargs .iteritems ():
4350 if not hasattr (v , "__call__" ):
44- # Not interested in non-callables.
51+ # "We" are not interested in non-callables.
4552 continue
4653 attach_record (k , v )
4754 if login_credentials is None :
0 commit comments