本文整理汇总了Golang中github.com/astaxie/beego/context.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ViewLogin
func ViewLogin(c *context.Context) {
cookie := c.GetCookie("MtimeCIUserId")
if len(cookie) <= 0 {
c.Redirect(302, "/login?url="+url.QueryEscape(c.Input.Uri()))
}
beego.Informational(cookie)
}
示例2: HandleAccess
func HandleAccess(ctx *context.Context) {
token := ctx.GetCookie("epic_user_token")
ok, _ := tools.VerifyToken(token)
if len(token) != 0 && ok {
ctx.Redirect(302, "/succeed")
}
}
示例3: LoginUserFromRememberCookie
func LoginUserFromRememberCookie(user *models.User, ctx *context.Context) (success bool) {
userName := ctx.GetCookie(setting.CookieUserName)
if len(userName) == 0 {
return false
}
defer func() {
if !success {
DeleteRememberCookie(ctx)
}
}()
user.UserName = userName
if err := user.Read("UserName"); err != nil {
return false
}
secret := utils.EncodeMd5(user.Rands + user.Password)
value, _ := ctx.GetSecureCookie(secret, setting.CookieRememberName)
if value != userName {
return false
}
LoginUser(user, ctx, true)
return true
}
示例4: FilterUser
func FilterUser(ctx *context.Context) {
if (ctx.Input.IsGet() || ctx.Input.IsPost()) && (ctx.Input.URL() == loginPageURL || ctx.Input.URL() == logoutPageURL) {
// Don't redirect itself to prevent the circle
} else {
user, ok := ctx.Input.Session("user").(*rbac.User)
if ok == false {
if guiMessage := guimessagedisplay.GetGUIMessageFromContext(ctx); guiMessage != nil {
guiMessage.AddDanger("Unauthorized")
}
ctx.Redirect(302, loginPageURL)
} else {
// Authorize
if user.HasPermission(componentName, ctx.Input.Method(), ctx.Input.URL()) == false {
if guiMessage := guimessagedisplay.GetGUIMessageFromContext(ctx); guiMessage != nil {
guiMessage.AddDanger("User is not authorized to this page. Please use another user with priviledge.")
}
ctx.Redirect(302, loginPageURL)
}
// Resource check is in another place since GUI doesn't place the resource name in url
// Audit log
go func() {
sendAuditLog(ctx, user.Name, true)
}()
}
}
}
示例5: LoginUserFromRememberCookie
func (this *BaseController) LoginUserFromRememberCookie(u *user.User, ctx *context.Context) (success bool) {
userName := ctx.GetCookie(setting.CookieUsername)
if len(userName) == 0 {
return false
}
defer func() {
if !success {
this.DeleteRememberCookie(ctx)
}
}()
u.Username = userName
if err := this.UserService.Read(u, "Username"); err != nil {
return false
}
secret := utils.EncodeMd5(u.PasswordSalt + u.Password)
value, _ := ctx.GetSecureCookie(secret, setting.CookieRememberName)
if value != userName {
return false
}
this.LoginUserRememberCookie(u, ctx, true)
return true
}
示例6: Handler
// Handler beego filter handler for serve captcha image
func (c *Captcha) Handler(ctx *context.Context) {
var chars []byte
id := path.Base(ctx.Request.RequestURI)
if i := strings.Index(id, "."); i != -1 {
id = id[:i]
}
key := c.key(id)
if len(ctx.Input.Query("reload")) > 0 {
chars = c.genRandChars()
if err := c.store.Put(key, chars, c.Expiration); err != nil {
ctx.Output.SetStatus(500)
ctx.WriteString("captcha reload error")
beego.Error("Reload Create Captcha Error:", err)
return
}
} else {
if v, ok := c.store.Get(key).([]byte); ok {
chars = v
} else {
ctx.Output.SetStatus(404)
ctx.WriteString("captcha not found")
return
}
}
img := NewImage(chars, c.StdWidth, c.StdHeight)
if _, err := img.WriteTo(ctx.ResponseWriter); err != nil {
beego.Error("Write Captcha Image Error:", err)
}
}
示例7: Render
// Render takes a Beego context, template name and a Context (map[string]interface{}).
// The template is parsed and cached, and gets executed into beegoCtx's ResponseWriter.
//
// Templates are looked up in `templates/` instead of Beego's default `views/` so that
// Beego doesn't attempt to load and parse our templates with `html/template`.
func Render(beegoCtx *context.Context, tmpl string, ctx Context) error {
template, err := p2.FromCache(path.Join(templateDir, tmpl))
if err != nil {
panic(err)
}
var pCtx p2.Context
if ctx == nil {
pCtx = p2.Context{}
} else {
pCtx = p2.Context(ctx)
}
if xsrf, ok := beegoCtx.GetSecureCookie(beego.BConfig.WebConfig.XSRFKey, "_xsrf"); ok {
pCtx["_xsrf"] = xsrf
}
// Only override "flash" if it hasn't already been set in Context
if _, ok := ctx["flash"]; !ok {
if ctx == nil {
ctx = Context{}
}
ctx["flash"] = readFlash(beegoCtx)
}
return template.ExecuteWriter(pCtx, beegoCtx.ResponseWriter)
}
示例8: checkLogin
// 检测登录,跳转登录界面
func checkLogin(c *context.Context) {
uid, ok := c.Input.Session("uid").(int64)
if !ok || uid <= 0 {
if c.Request.RequestURI != "/login" {
c.Redirect(302, "/login")
}
}
}
示例9: recoverPanic
func (p *ControllerRegistor) recoverPanic(context *beecontext.Context) {
if err := recover(); err != nil {
if err == USERSTOPRUN {
return
}
if RunMode == "dev" {
if !RecoverPanic {
panic(err)
} else {
if ErrorsShow {
if handler, ok := ErrorMaps[fmt.Sprint(err)]; ok {
executeError(handler, context)
return
}
}
var stack string
Critical("the request url is ", context.Input.Url())
Critical("Handler crashed with error", err)
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
Critical(fmt.Sprintf("%s:%d", file, line))
stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
}
showErr(err, context, stack)
}
} else {
if !RecoverPanic {
panic(err)
} else {
// in production model show all infomation
if ErrorsShow {
if handler, ok := ErrorMaps[fmt.Sprint(err)]; ok {
executeError(handler, context)
return
} else if handler, ok := ErrorMaps["503"]; ok {
executeError(handler, context)
return
} else {
context.WriteString(fmt.Sprint(err))
}
} else {
Critical("the request url is ", context.Input.Url())
Critical("Handler crashed with error", err)
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
Critical(fmt.Sprintf("%s:%d", file, line))
}
}
}
}
}
}
示例10: FilterAuth
// FilterAuth prevents the user from accessing protected pages if they are not
// logged in.
func FilterAuth(ctx *context.Context) {
if ctx.Input.GetData("user") == nil {
ctx.Redirect(302, fmt.Sprintf(
"%s?redirect=%s",
beego.URLFor("UserController.Login"),
url.QueryEscape(ctx.Request.URL.Path),
))
}
}
示例11: GetLoginRedirect
// get login redirect url from cookie
func GetLoginRedirect(ctx *context.Context) string {
loginRedirect := strings.TrimSpace(ctx.GetCookie("login_to"))
if utils.IsMatchHost(loginRedirect) == false {
loginRedirect = "/"
} else {
ctx.SetCookie("login_to", "", -1, "/")
}
return loginRedirect
}
示例12: IsLogin
func IsLogin(ctx *context.Context) bool {
username := ctx.GetCookie("username")
password := ctx.GetCookie("password")
if beego.AppConfig.String("username") == username &&
beego.AppConfig.String("password") == password {
return true
}
return false
}
示例13: WriteJson
func WriteJson(ctx *context.Context, i interface{}) error {
data, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
return err
}
ctx.WriteString(string(data))
return nil
}
示例14: Write
func (me *StatusCode) Write(ctx *context.Context, code EUmsError, err error) {
me.Code = int(code)
j, _ := json.Marshal(me)
ctx.WriteString(string(j))
beego.Info(string(j), code.ToString(), err)
}
示例15: loginFilter
func loginFilter(ctx *context.Context) {
user := ctx.Input.CruSession.Get("user")
//user := this.GetSession("user")
fmt.Println("loginFilter user")
fmt.Println(user)
if user == nil && ctx.Request.RequestURI != "/" && ctx.Request.RequestURI != "/login" {
ctx.Redirect(302, "/")
}
}