本文整理汇总了Golang中appengine/user.LoginURL函数的典型用法代码示例。如果您正苦于以下问题:Golang LoginURL函数的具体用法?Golang LoginURL怎么用?Golang LoginURL使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LoginURL函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: authOnly
func authOnly(next myHandler) myHandler {
return func(ctx *Context, w http.ResponseWriter, r *http.Request) error {
if ctx.user == nil {
url, err := user.LoginURL(ctx.c, "/login")
if err != nil {
return err
}
ctx.SetTitle("Please log in to continue ...")
return ctx.Render(signinTmpl, url)
}
err := ctx.LoadUserSession()
if err != nil {
return err
}
if ctx.userSession == nil {
url, err := user.LoginURL(ctx.c, "/login")
if err != nil {
return err
}
ctx.SetTitle("Invalid user ...")
return ctx.Render(signinTmpl, url)
}
return next(ctx, w, r)
}
}
示例2: adminHandler
func adminHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/admin/" {
errorHandler(w, r, http.StatusNotFound)
return
}
admin := template.Must(template.ParseFiles(
"http/html/_base.html",
"http/html/admin.html",
))
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
if !u.Admin {
url, _ := user.LoginURL(c, r.URL.String())
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
admin.Execute(w, nil)
}
示例3: admin
func admin(w http.ResponseWriter, r *http.Request) {
// handle requests to "/admin/"
c := appengine.NewContext(r)
billQuery := datastore.NewQuery("Bill").Order("-Session").Order("-Number")
bills := make([]bill.Bill, 0)
if _, err := billQuery.GetAll(c, &bills); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
senatorQuery := datastore.NewQuery("Senator").Order("-Name")
senators := make([]senator.Senator, 0)
if _, err := senatorQuery.GetAll(c, &senators); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
login, _ := user.LoginURL(c, "/")
logout, _ := user.LogoutURL(c, "/")
pageInfo := PageInfo{Title: "Administrator Dashboard",
User: user.Current(c),
Admin: user.IsAdmin(c),
LoginURL: login,
LogoutURL: logout,
Bills: bills,
Senators: senators}
if err := adminTemplate.Execute(w, pageInfo); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
示例4: webuserOK
func webuserOK(c appengine.Context, w http.ResponseWriter, r *http.Request) bool {
if !userauthenticated(c) {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return false
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return false
}
u := user.Current(c)
authzed, err := userauthorized(c, u.Email)
if err != nil {
c.Errorf("authorization error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return false
}
if !authzed {
c.Warningf("authorization failure: %v", u.Email)
w.WriteHeader(http.StatusForbidden)
err = templates.ExecuteTemplate(w, "unauthorized.html", nil)
if err != nil {
c.Errorf("unauthorized user and got err on template: %v", err)
}
return false
}
return true
}
示例5: createPageHeader
func createPageHeader(title string, w http.ResponseWriter, r *http.Request) *PageHeader {
pageHeader := &PageHeader{Title: title}
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
panic("user.LoginURL error: " + err.Error())
}
pageHeader.UserURL = url
pageHeader.UserLabel = "Login"
pageHeader.IsAdmin = false
} else {
url, err := user.LogoutURL(c, r.URL.String())
if err != nil {
panic("user.LogoutURL error: " + err.Error())
}
pageHeader.UserURL = url
pageHeader.UserLabel = "Logout"
pageHeader.LoginMessage = "Hello, " + u.String() + "!"
pageHeader.IsAdmin = user.IsAdmin(c)
w.Header().Set("Pragma", "no-cache")
}
return pageHeader
}
示例6: require_login
// require_login is a wrapper for a function that serves web pages.
// By wrapping the function in require_login, the user is forced to
// log in to their Google account in order to access the system.
func require_login(H handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// Force the person to log in.
client := user.Current(c)
if client == nil {
U := strings.Split(r.URL.String(), "/")
V := U[len(U)-1]
url, err := user.LoginURL(c, V)
if err != nil {
http.Error(w, "Error",
http.StatusInternalServerError)
return
}
c.Infof(fmt.Sprintf("Login url: %s", url))
msg := "To use this site, you must be logged into your Google account."
return_msg := "Continue to login page"
Message_page(w, r, nil, msg, return_msg, url)
return
}
H(w, r)
}
}
示例7: AppstatsHandler
func AppstatsHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
if appengine.IsDevAppServer() {
// noop
} else if u := user.Current(c); u == nil {
if loginURL, err := user.LoginURL(c, r.URL.String()); err == nil {
http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
} else {
serveError(w, err)
}
return
} else if !u.Admin {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
if detailsURL == r.URL.Path {
Details(w, r)
} else if fileURL == r.URL.Path {
File(w, r)
} else if strings.HasPrefix(r.URL.Path, staticURL) {
Static(w, r)
} else {
Index(w, r)
}
}
示例8: handler
func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
loginUrl, _ := user.LoginURL(c, "/todo")
http.Redirect(w, r, loginUrl, http.StatusFound)
return
}
// start 2 OMIT
t, err := template.ParseFiles("todo/todo.tmpl") // テンプレート読み込み // HL
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-type", "text/html; charset=utf-8")
logoutUrl, _ := user.LogoutURL(c, "/")
params := struct {
LogoutUrl string
User *user.User
}{
logoutUrl,
u,
}
err = t.Execute(w, params) // テンプレート適用! // HL
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// end 2 OMIT
}
示例9: NewParams
// NewParams returns a new initialized Params
func NewParams(r *http.Request, page *Page) *Params {
now := time.Now()
c := appengine.NewContext(r)
p := Params{Pages: Pages, PathPageMap: PathPageMap, NamePageMap: NamePageMap,
Request: r, Mobile: isMobile(r), Page: page, Start: now, AppCtx: c,
Custom: make(map[string]interface{})}
p.User = user.Current(c)
if p.User == nil {
p.LoginURL, p.Err = user.LoginURL(c, r.URL.String())
} else {
p.LogoutURL, p.Err = user.LogoutURL(c, r.URL.String())
if p.Err != nil {
// Log if error, but this is not a show-stopper:
c.Errorf("Error getting logout URL: %s", p.Err.Error())
}
p.Account, p.Err = cache.GetAccount(p.Request, c, p.User)
}
if p.Err != nil {
c.Errorf("ERROR: %s", p.Err.Error())
}
return &p
}
示例10: GetGameInfo
func GetGameInfo(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
c.Infof("Not logged in")
url, _ := user.LoginURL(c, "/")
fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
return
}
if !users.IsUserSignedIn(c) {
fmt.Fprintf(w, "Not Registerd")
return
}
id, pErr := strconv.ParseInt(r.FormValue("game_id"), 10, 32)
if pErr != nil {
c.Infof("Error parseing int: %v", pErr)
http.Error(w, pErr.Error(), http.StatusInternalServerError)
return
}
mGame, _, err := GetGame(c, int(id))
if err != nil {
c.Infof("Error getting game: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
mess, jErr := json.Marshal(mGame)
if jErr != nil {
c.Infof("Error json marshal: %v", jErr)
http.Error(w, jErr.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, string(mess))
}
示例11: ensureProfile
func ensureProfile(w http.ResponseWriter, r *http.Request) (*datastore.Key, *Profile) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
check(err)
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return nil, nil
}
q := datastore.NewQuery("Profile").Filter("Username=", u.String()).Limit(1)
i := q.Run(c)
var p Profile
key, _ := i.Next(&p)
if key != nil {
return key, &p
}
p = Profile{Username: u.String()}
_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Profile", nil), &p)
check(err)
w.Header().Set("Location", r.URL.String())
w.WriteHeader(http.StatusFound)
return nil, nil
}
示例12: registration
func registration(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := User{
Name: "TestHendrik",
StartDate: datastore.SecondsToTime(time.Seconds()),
}
if g := user.Current(c); g != nil {
var u2 User
u.Account = g.String()
if err := datastore.Get(c, datastore.NewKey("user", g.String(), 0, nil), &u2); err == datastore.ErrNoSuchEntity {
key, err := datastore.Put(c, datastore.NewKey("user", u.Account, 0, nil), &u)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "User %q stored %q", u.Account, key)
return
} else {
fmt.Fprintf(w, "User %q is already logged in", g.String())
return
}
} else {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
}
示例13: Root
func Root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
var u *user.User
if u = user.Current(c); u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
// Get last numEntries entries & display them
q := datastore.NewQuery("Entry").Ancestor(userKey(u.Email, c)).Order("-Time").Limit(numEntries)
entries := make([]entry.Entry, 0, numEntries)
if _, err := q.GetAll(c, &entries); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for i := 0; i < len(entries)/2; i++ {
entries[i], entries[len(entries)-i-1] = entries[len(entries)-i-1], entries[i]
}
for i, _ := range entries {
entries[i].Time = entries[i].Time.Round(10 * time.Millisecond)
}
if err := homeTemplate.Execute(w, map[string]interface{}{
"User": u.Email,
"Entries": entries}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
示例14: AdminHandler
func AdminHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
db := DB{c}
adminUser := user.Current(c)
if adminUser == nil {
url, _ := user.LoginURL(c, "/admin/dashboard")
http.Redirect(w, r, url, 301)
return
}
if r.Method == "GET" {
dir := path.Join(os.Getenv("PWD"), "templates")
admin := path.Join(dir, "admin.html")
data := map[string]interface{}{"user": adminUser.String(), "config": Config}
page := mustache.RenderFile(admin, data)
fmt.Fprint(w, page)
} else if r.Method == "POST" {
if len(r.FormValue("v")) == 0 {
return
}
switch r.FormValue("op") {
case "UnqueueUser":
db.Context.Infof("unqueuing %v", r.FormValue("v"))
db.UnqueueUser(r.FormValue("v"))
case "BatchUsers":
users := strings.Split(r.FormValue("v"), ",")
for _, v := range users {
QueueUser(v, c)
}
}
fmt.Fprintf(w, "{\"op\":\"%v\",\"success\":true}", r.FormValue("op"))
}
}
示例15: root
func root(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" || r.URL.Path != "/" {
serve404(w)
return
}
c := appengine.NewContext(r)
u := user.Current(c)
gg, err := brg.GetBuriggies(c, 100)
if err != nil {
serveError(c, w, err)
return
}
log.Println("hoi")
loginUrl, _ := user.LoginURL(c, "/")
logoutUrl, _ := user.LogoutURL(c, "/")
mdi := modelIndex{Buriggies: gg, User: u, LoginUrl: loginUrl, LogoutUrl: logoutUrl}
t, errTpl := template.ParseFiles("code/main.tpl")
if errTpl != nil {
serveError(c, w, errTpl)
return
}
if err := t.Execute(w, mdi); err != nil {
c.Errorf("%v", err)
}
}