本文整理汇总了Golang中github.com/revel/revel.Controller类的典型用法代码示例。如果您正苦于以下问题:Golang Controller类的具体用法?Golang Controller怎么用?Golang Controller使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Controller类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: AuthInterceptor
func AuthInterceptor(c *revel.Controller) revel.Result {
// 全部变成首字大写
/*
var controller = strings.Title(c.Name)
var method = strings.Title(c.MethodName)
// 是否需要验证?
if !needValidate(controller, method) {
return nil
}
*/
// 验证是否已登录
// 必须是管理员
if username, ok := c.Session["Username"]; ok && username == configService.GetAdminUsername() {
return nil // 已登录
}
// 没有登录, 判断是否是ajax操作
if c.Request.Header.Get("X-Requested-With") == "XMLHttpRequest" {
re := info.NewRe()
re.Msg = "NOTLOGIN"
return c.RenderJson(re)
}
return c.Redirect("/login")
}
开发者ID:ClaudeXin,项目名称:leanote,代码行数:26,代码来源:init.go
示例2: MSessionFilter
func MSessionFilter(c *revel.Controller, fc []revel.Filter) {
sessionId := c.Session.Id()
// 从memcache中得到cache, 赋给session
cache := revel.Session(memcache.GetMap(sessionId))
Log("memcache")
LogJ(cache)
if cache == nil {
cache = revel.Session{}
cache.Id()
}
c.Session = cache
// Make session vars available in templates as {{.session.xyz}}
c.RenderArgs["session"] = c.Session
fc[0](c, fc[1:])
// 再把session保存之
LogJ(c.Session)
memcache.SetMap(sessionId, c.Session, -1)
// 只留下sessionId
c.Session = revel.Session{revel.SESSION_ID_KEY: sessionId}
}
开发者ID:intZz,项目名称:leanote,代码行数:26,代码来源:MSession.go
示例3: AuthFilter
/*
Filter AuthFilter is Revel Filter for JWT Auth Token verification
Register it in the revel.Filters in <APP_PATH>/app/init.go
Add jwt.AuthFilter anywhere deemed appropriate, it must be register after revel.PanicFilter
revel.Filters = []revel.Filter{
revel.PanicFilter,
...
jwt.AuthFilter, // JWT Auth Token verification for Request Paths
...
}
Note: If everything looks good then Claims map made available via c.Args
and can be accessed using c.Args[jwt.TOKEN_CLAIMS_KEY]
*/
func AuthFilter(c *revel.Controller, fc []revel.Filter) {
if !anonymousPaths.MatchString(c.Request.URL.Path) {
token, err := ParseFromRequest(c.Request.Request)
if err == nil && token.Valid && !IsInBlocklist(GetAuthToken(c.Request)) {
c.Args[TOKEN_CLAIMS_KEY] = token.Claims
fc[0](c, fc[1:]) // everything looks good, move on
} else {
if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
revel.ERROR.Println("That's not even a token")
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
revel.ERROR.Println("Timing is everything, Token is either expired or not active yet")
} else {
revel.ERROR.Printf("Couldn't handle this token: %v", err)
}
} else {
revel.ERROR.Printf("Couldn't handle this token: %v", err)
}
c.Response.Status = http.StatusUnauthorized
c.Response.Out.Header().Add("WWW-Authenticate", Realm)
c.Result = c.RenderJson(map[string]string{
"id": "unauthorized",
"message": "Invalid or token is not provided",
})
return
}
}
fc[0](c, fc[1:]) //not applying JWT auth filter due to anonymous path
}
开发者ID:oblank,项目名称:jwtauth,代码行数:49,代码来源:jwt.go
示例4: CheckUserAuth
func CheckUserAuth(controller *revel.Controller) revel.Result {
if controller.Action == "Static.Serve" ||
controller.Action == "App.Login" ||
controller.Action == "User.Login" ||
controller.Action == "User.Logout" {
return nil
}
var (
userAuth = new(security.UserAuth)
username string
sessionData = *security.GetSessionData(&controller.Session)
)
security.AuthCache.Get(controller.Session.Id(), userAuth)
if v, ok := sessionData["username"]; ok {
username = v.(string)
}
if userAuth != nil && username != "" && userAuth.Equal(security.UserAuthGenerate(controller.Request)) {
return nil
}
controller.Flash.Error("Please log in first")
controller.Response.Out.Header().Set("Requires-Auth", "1")
// controller.Response.Status = 401
return controller.Redirect((*User).Login)
}
开发者ID:digideskio,项目名称:watchdog_ui,代码行数:32,代码来源:user.go
示例5: adminOnly
func adminOnly(c *revel.Controller) revel.Result {
if c.Session["usertype"] == "ADMIN" {
return nil
}
c.Flash.Error(c.Message("access.message.notallowed"))
return c.Redirect(routes.App.Index())
}
开发者ID:bertzzie,项目名称:obrolansubuh-backend,代码行数:8,代码来源:init.go
示例6: checkUser
func checkUser(c *revel.Controller) revel.Result {
if _, ok := c.Session["user"]; ok {
return nil
}
c.Flash.Error(c.Message("login.message.notloggedin"))
return c.Redirect(routes.App.Login())
}
开发者ID:bertzzie,项目名称:obrolansubuh-backend,代码行数:8,代码来源:init.go
示例7: CheckLoginAdmin
func CheckLoginAdmin(c *revel.Controller) revel.Result {
if c.Session[LOGIN_USERID] == "" || models.Role(c.Session[LOGIN_USERROLE]) != models.ROLE_SUPER_ADMIN {
return c.Redirect(
revel.MainRouter.Reverse("Auth.Login", map[string]string{}).Url,
)
}
return nil
}
开发者ID:11101171,项目名称:whale,代码行数:8,代码来源:super.go
示例8: CheckLogin
// func init() {
// revel.InterceptFunc(CheckLogin, revel.BEFORE, &App{})
// }
func CheckLogin(c *revel.Controller) revel.Result {
if c.Session[LOGIN_USERID] == "" {
return c.Redirect(
revel.MainRouter.Reverse("Auth.Login", map[string]string{}).Url,
)
}
return nil
}
开发者ID:11101171,项目名称:whale,代码行数:11,代码来源:super.go
示例9: Search
// search certain content
func Search(key string, c *revel.Controller) revel.Result {
var problems []models.Problem
err := engine.Where("title = ? ", key).Find(&problems)
if err != nil {
c.Flash.Error("error %s", err.Error())
c.Redirect(routes.Notice.Crash())
}
return c.Render(problems)
}
开发者ID:jango2015,项目名称:OJ,代码行数:10,代码来源:util.go
示例10: redirectAuthenticationPageForAdmin
func redirectAuthenticationPageForAdmin(c *revel.Controller) revel.Result {
uidStr := c.Session["uid"]
uid, _ := strconv.Atoi(uidStr)
errorPage := c.RenderTemplate("errors/401.html")
if len(uidStr) == 0 {
return errorPage
}
if (models.User{}.FetchUserByUserId(uint(uid)).Status != models.UserAdmin) {
return errorPage
}
return nil
}
开发者ID:randyumi,项目名称:kuchikommi,代码行数:12,代码来源:init.go
示例11: checkDataTypeParam
func checkDataTypeParam(c *revel.Controller) revel.Result {
dataType, ok := c.Params.Values["dataType"]
if ok && dataType[0] != "" {
if _, ok := database.SynchronizationTypes[dataType[0]]; !ok {
c.Response.Status = 400
return c.RenderJson("wrong dataType attribute")
}
return nil
}
c.Response.Status = 400
return c.RenderJson("mandatory parameter dataType is not present")
}
开发者ID:rtm7777,项目名称:rozklad_cdtu,代码行数:12,代码来源:synchronization.go
示例12: returnMessage
func returnMessage(c *revel.Controller, message interface{}, err error) revel.Result {
result := &opResult{}
if err != nil {
result.Result = Error
result.Message = err.Error()
revel.WARN.Fatalln(err)
} else {
result.Result = Success
result.Message = message
}
return c.RenderJson(result)
}
开发者ID:qtzheng,项目名称:SIMP,代码行数:13,代码来源:basecollection.go
示例13: ServeStatic
func ServeStatic(uri, contentType string, c *revel.Controller) {
filePath := GetFilePath(uri)
revel.INFO.Printf("read static file %s\n", filePath)
file, err := os.Open(filePath)
defer file.Close()
if err != nil {
c.Result = CommonResult{Data: []byte(`sorry file not found`)}
return
}
data, err := ioutil.ReadAll(file)
if err != nil {
c.Result = CommonResult{Data: []byte(`sorry file not found`)}
return
}
c.Result = CommonResult{Data: data, ContentType: contentType}
}
开发者ID:wangboo,项目名称:gutil,代码行数:16,代码来源:asset.go
示例14: authenticate
//authentication check
func authenticate(c *revel.Controller) revel.Result {
if inStringSlice(strings.ToLower(c.Action),
adminPermission) {
if !adminAuthentication(c) {
c.Flash.Error("you are not admin")
return c.Redirect("/")
}
}
if inStringSlice(strings.ToLower(c.Action),
userPermission) {
if ok := connected(c); !ok {
c.Flash.Error("please login first")
return c.Redirect(routes.Account.Login())
} else {
return nil
}
}
if inStringSlice(strings.ToLower(c.Action),
logoutCheck) {
if ok := connected(c); ok {
c.Flash.Error("can not repeat login")
return c.Redirect("/")
} else {
return nil
}
}
return nil
}
开发者ID:jango2015,项目名称:OJ,代码行数:29,代码来源:util.go
示例15: CheckLogin
//检测登陆
func CheckLogin(c *revel.Controller) revel.Result {
//登陆页面,CSS, JS, Ajax, 验证码页面 都不进行登陆验证
if c.Name == "User" && c.MethodName == "Login" || c.Name == "Ajax" || c.Name == "Static" || c.Name == "Captcha" || c.Name == "Kindeditor" {
if LANG, ok := c.Session["Lang"]; ok {
//设置语言
c.RenderArgs["currentLocale"] = LANG
} else {
//设置默认语言
c.RenderArgs["currentLocale"] = "zh"
}
return nil
} else {
UserID := utils.GetSession("UserID", c.Session)
if len(UserID) > 0 {
UserID, err := strconv.ParseInt(UserID, 10, 64)
if err != nil {
revel.WARN.Println(err)
return c.Redirect("/Login/")
}
admin := new(models.Admin)
admin_info := admin.GetById(UserID)
if admin_info.Id <= 0 {
return c.Redirect("/Login/")
}
//控制器
c.RenderArgs["Controller"] = c.Name
//动作
c.RenderArgs["action"] = c.Action
//模型
c.RenderArgs["Model"] = c.MethodName
//登陆信息
c.RenderArgs["admin_info"] = admin_info
//设置语言
c.RenderArgs["currentLocale"] = admin_info.Lang
} else {
//控制器
c.RenderArgs["Controller"] = c.Name
//动作
c.RenderArgs["action"] = c.Action
//模型
c.RenderArgs["Model"] = c.MethodName
return c.Redirect("/Login/")
}
}
return nil
}
开发者ID:chinanjjohn2012,项目名称:GoCMS,代码行数:59,代码来源:init.go
示例16: SessionFilter
// SessionFilter is a Revel Filter that retrieves and sets the session cookie.
// Within Revel, it is available as a Session attribute on Controller instances.
// The name of the Session cookie is set as CookiePrefix + "_SESSION".
func SessionFilter(c *revel.Controller, fc []revel.Filter) {
session := restoreSession(c.Request.Request)
// c.Session, 重新生成一个revel.Session给controller!!!
// Log("sessoin--------")
// LogJ(session)
revelSession := revel.Session(session) // 强制转换 还是同一个对象, 但有个问题, 这样Session.Id()方法是用revel的了
c.Session = revelSession
// 生成sessionId
c.Session.Id()
sessionWasEmpty := len(c.Session) == 0
// Make session vars available in templates as {{.session.xyz}}
c.RenderArgs["session"] = c.Session
fc[0](c, fc[1:])
// Store the signed session if it could have changed.
if len(c.Session) > 0 || !sessionWasEmpty {
// 转换成lea.Session
session = Session(c.Session)
c.SetCookie(session.cookie())
}
}
开发者ID:intZz,项目名称:leanote,代码行数:26,代码来源:session.go
示例17: checkRole
func checkRole(this *revel.Controller) revel.Result {
pv := models.PV{
IP: this.Request.Host,
Page: this.Action,
TimeStamp: time.Now().Format("2006-01-02 15:04:05")}
addPV(pv)
// 设置游客的访问权限
if this.Session["administrator"] == "" {
if this.Action == "Admin.SignIn" ||
this.Action == "Picture.Show" ||
this.Action == "Picture.Search" ||
this.Action == "Picture.AddComment" ||
this.Action == "Picture.UploadForCheck" ||
this.Action == "Picture.PostUploadForCheck" ||
this.Action == "Picture.UniversityPictureNums" {
return nil
} else {
this.Flash.Success("需要管理员身份才能访问该页面。")
log.Println("游客访问了 " + this.Action + " 页面,已自动跳转到首页")
return this.Redirect(controllers.App.Index)
}
}
// 设置管理员的访问权限
if this.Session["administrator"] == "true" {
if this.Action == "Admin.SignIn" {
this.Flash.Success("您已经登录,请先注销再登录。")
log.Println("管理员访问了登录页面,已自动跳转到首页")
return this.Redirect(controllers.App.Index)
} else {
log.Println("管理员 " + this.Session["administrator"] + " 访问了 " + this.Action + " 页面")
return nil
}
}
return this.Redirect(controllers.Admin.SignIn)
}
开发者ID:xausee,项目名称:symbol,代码行数:38,代码来源:intercepters.go
示例18: RouterFilter
func RouterFilter(c *revel.Controller, fc []revel.Filter) {
// 补全controller部分
path := c.Request.Request.URL.Path
// Figure out the Controller/Action
var route *revel.RouteMatch = revel.MainRouter.Route(c.Request.Request)
if route == nil {
c.Result = c.NotFound("No matching route found: " + c.Request.RequestURI)
return
}
// The route may want to explicitly return a 404.
if route.Action == "404" {
c.Result = c.NotFound("(intentionally)")
return
}
//----------
// life start
/*
type URL struct {
Scheme string
Opaque string // encoded opaque data
User *Userinfo // username and password information
Host string // host or host:port
Path string
RawQuery string // encoded query values, without '?'
Fragment string // fragment for references, without '#'
}
*/
if route.ControllerName != "Static" {
// api设置
// leanote.com/api/user/get => ApiUser::Get
//* /api/login ApiAuth.Login, 这里的设置, 其实已经转成了ApiAuth了
if strings.HasPrefix(path, "/api") && !strings.HasPrefix(route.ControllerName, "Api") {
route.ControllerName = "Api" + route.ControllerName
} else if strings.HasPrefix(path, "/member") && !strings.HasPrefix(route.ControllerName, "Member") {
// member设置
route.ControllerName = "Member" + route.ControllerName
}
// end
}
// Set the action.
if err := c.SetAction(route.ControllerName, route.MethodName); err != nil {
c.Result = c.NotFound(err.Error())
return
}
// Add the route and fixed params to the Request Params.
c.Params.Route = route.Params
// Add the fixed parameters mapped by name.
// TODO: Pre-calculate this mapping.
for i, value := range route.FixedParams {
if c.Params.Fixed == nil {
c.Params.Fixed = make(url.Values)
}
if i < len(c.MethodType.Args) {
arg := c.MethodType.Args[i]
c.Params.Fixed.Set(arg.Name, value)
} else {
revel.WARN.Println("Too many parameters to", route.Action, "trying to add", value)
break
}
}
fc[0](c, fc[1:])
}
开发者ID:intZz,项目名称:leanote,代码行数:69,代码来源:Route.go
示例19: GetGridJson
func GetGridJson(c *revel.Controller, count int, data interface{}) revel.Result {
json := &GridJson{count, data}
return c.RenderJson(json)
}
开发者ID:qtzheng,项目名称:SIMP,代码行数:4,代码来源:basecollection.go
示例20: FilterForApiDoc
func FilterForApiDoc(c *revel.Controller, fc []revel.Filter) {
if record, _ := revel.Config.Bool("yaag.record"); !record {
fc[0](c, fc[1:])
return
}
w := httptest.NewRecorder()
c.Response = revel.NewResponse(w)
httpVerb := c.Request.Method
customParams := make(map[string]interface{})
headers := make(map[string]string)
hasJson := false
hasXml := false
body := middleware.ReadBody(c.Request.Request)
if c.Request.ContentType == "application/json" {
if httpVerb == "POST" || httpVerb == "PUT" || httpVerb == "PATCH" {
err := json.Unmarshal([]byte(*body), &customParams)
if err != nil {
log.Println("Json Error ! ", err)
} else {
hasJson = true
}
} else {
err := json.Unmarshal([]byte(c.Request.URL.RawQuery), &customParams)
if err != nil {
log.Println("Json Error ! ", err)
} else {
hasJson = true
}
}
} else if c.Request.ContentType == "application/xml" {
if httpVerb == "POST" || httpVerb == "PUT" || httpVerb == "PATCH" {
err := xml.Unmarshal([]byte(*body), &customParams)
if err != nil {
log.Println("Xml Error ! ", err)
} else {
hasXml = true
}
} else {
err := xml.Unmarshal([]byte(c.Request.URL.RawQuery), &customParams)
if err != nil {
log.Println("Json Error ! ", err)
} else {
hasXml = true
}
}
}
log.Println(hasJson, hasXml)
// call remaiing filters
fc[0](c, fc[1:])
c.Result.Apply(c.Request, c.Response)
htmlValues := yaag.APICall{}
htmlValues.CommonRequestHeaders = make(map[string]string)
// get headers
for k, v := range c.Request.Header {
isCommon := false
for _, hk := range yaag.CommonHeaders {
if k == hk {
isCommon = true
htmlValues.CommonRequestHeaders[k] = strings.Join(v, " ")
break
}
}
if !isCommon {
headers[k] = strings.Join(v, " ")
}
}
htmlValues.MethodType = httpVerb
htmlValues.CurrentPath = c.Request.URL.Path
htmlValues.PostForm = make(map[string]string)
for k, v := range c.Params.Form {
htmlValues.PostForm[k] = strings.Join(v, " ")
}
htmlValues.RequestBody = *body
htmlValues.RequestHeader = headers
htmlValues.RequestUrlParams = make(map[string]string)
for k, v := range c.Request.URL.Query() {
htmlValues.RequestUrlParams[k] = strings.Join(v, " ")
}
htmlValues.ResponseHeader = make(map[string]string)
htmlValues.ResponseBody = w.Body.String()
for k, v := range w.Header() {
htmlValues.ResponseHeader[k] = strings.Join(v, " ")
}
htmlValues.ResponseCode = w.Code
yaag.ApiCallValueInstance.BaseLink = c.Request.Host
go yaag.GenerateHtml(&htmlValues)
}
开发者ID:dfmonaco,项目名称:yaag,代码行数:95,代码来源:filter.go
注:本文中的github.com/revel/revel.Controller类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论