Beispiel #1
0
func setupAPIGates(app *server.App) {
	var API = app.API()

	// --------------------------------------------------------------------------
	// Gate
	API.GET("/gates/",
		server.Handler(func(req *wcg.Request) response.Response {
			return response.NewJSONResponse(gates.GetAll(req))
		}))

	API.GET("/gates/:key.json",
		server.Handler(func(req *wcg.Request) response.Response {
			val := gates.Get(req, req.Param("key"))
			if val == nil {
				return response.NotFound(req)
			} else {
				return response.NewJSONResponse(val)
			}
		}))

	API.PUT("/gates/:key.json",
		middleware.ParseForm(nil),
		server.Handler(func(req *wcg.Request) response.Response {
			val := gates.Get(req, req.Param("key"))
			if val == nil {
				return response.NotFound(req)
			} else {
				val.UIDs = req.HTTPRequest().Form["uids"]
				gates.PutMulti(req, val)
				return response.NewJSONResponse(val)
			}
		}))
}
Beispiel #2
0
// TemporaryAllow is a syntax to allow the user to pass the gate `key`.
func TemporaryAllow(req *httptest.TestRequest, key string, f func()) {
	// new user
	var user wcg.User
	if req.Request.User == nil {
		user = &TestUser{
			id: wcg.NewUUID(),
		}
		req.Request.User = user
	} else {
		user = req.Request.User
	}

	var g *models.Gate
	_, value := entities.Gate.Get().Key(key).Cache(true).MustOne(req.Request)
	if value == nil {
		if !gates.Exists(key) {
			req.Request.Logger.Warnf("You are allowing %q gate key temporary on the request but that gate key does not exist.", key)
		}
		g = &models.Gate{
			Key: key,
		}
	} else {
		g = value.(*models.Gate)
	}
	oldUIDs := g.UIDs
	g.UIDs = append(g.UIDs, user.ID())

	gates.PutMulti(req.Request, g)

	defer func() {
		if logging {
			req.Request.Logger.Infof("<< TemporaryAllow %q", key)
		}
		g.UIDs = oldUIDs
		gates.PutMulti(req.Request, g)
	}()

	if logging {
		req.Request.Logger.Infof(">> TemporaryAllow %q", key)
	}
	f()
}