授权功能状态中间件, status 和 utils.PARAMS.__STATUS 两个不同的指针, 指向同一个内存地址.
(status *bool)
| 111 | |
| 112 | // 授权功能状态中间件, status 和 utils.PARAMS.__STATUS 两个不同的指针, 指向同一个内存地址. |
| 113 | func AuthorizeStatusMiddleware(status *bool) func(http.HandlerFunc) http.HandlerFunc { |
| 114 | return func(next http.HandlerFunc) http.HandlerFunc { |
| 115 | return func(w http.ResponseWriter, r *http.Request) { |
| 116 | // 判断用户是否有登录锁, 若有锁, 则重定向到 login.html 页面. |
| 117 | user := strings.Split(r.RemoteAddr, ":")[0] + "+" + r.Header.Get("User-Agent") |
| 118 | if AuthorizeUser(user) { |
| 119 | http.Redirect(w, r, "/login/", http.StatusMovedPermanently) |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | // 功能是打开的还是禁用的. |
| 124 | if !(*status) { |
| 125 | http.Error(w, "[* HTTP 403]: forbidden.", http.StatusForbidden) |
| 126 | return |
| 127 | } |
| 128 | |
| 129 | // 必须通过 POST 请求进行操作. |
| 130 | if r.Method != http.MethodPost { |
| 131 | http.Error(w, "[* HTTP 405]: only POST request is allowed.", http.StatusMethodNotAllowed) |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | next(w, r) |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 |
no test coverage detected