作者:ilkk
项目:go-playgroun
func root(w http.ResponseWriter, r *http.Request) {
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
}
q := datastore.NewQuery("Greeting").Order("-Timestamp").Limit(10)
greetings := make([]Greeting, 0, 10)
if _, err := q.GetAll(c, &greetings); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url, _ := user.LogoutURL(c, "/")
if err := guestBookTemplate.Execute(w,
struct {
LogoutUrl string
Greetings []Greeting
}{url, greetings}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
作者:foolusio
项目:nhlstat
func newgame(w http.ResponseWriter, r *http.Request) {
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
}
url, err := user.LogoutURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
i := Index{
User: u.Email,
Login: false,
URL: url,
}
if err := newGameTemplate.Execute(w, i); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
作者:aaronlindse
项目:tamu-student-senate-ap
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)
}
}
作者:kunni
项目:roegebeer
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)
}
}
作者:nicko9
项目:Chrome-Infr
func ninjalogForm(w http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
ninjalogUpload(w, req)
return
}
ctx := appengine.NewContext(req)
u := user.Current(ctx)
login, err := user.LoginURL(ctx, "/ninja_log/")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
logout, err := user.LogoutURL(ctx, "/ninja_log/")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
data := struct {
User *user.User
Login string
Logout string
}{
User: u,
Login: login,
Logout: logout,
}
err = formTmpl.Execute(w, data)
if err != nil {
ctx.Errorf("formTmpl: %v", err)
}
}
作者:hpaluc
项目:gae-blog-g
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
}
作者:hajimehosh
项目:kakeib
func filterUsers(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
w.Header().Set(
"Content-type",
"text/plain; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "'login: required' is needed at app.yaml.\n")
return
}
if _, ok := permittedUserEmails[u.Email]; !ok {
w.Header().Set(
"Content-type",
"text/html; charset=utf-8")
url, _ := user.LogoutURL(c, "/")
w.WriteHeader(http.StatusForbidden)
forbiddenTmpl.Execute(w, map[string]interface{}{
"LogoutURL": url,
})
return
}
f(w, r)
}
}
作者:javie
项目:gocon
// NewPage returns a new Page initialized embedding the template with the
// given name and data, the current user for the given context, and the
// latest announcement.
func NewPage(ctx appengine.Context, name string, data interface{}) (*Page, error) {
p := &Page{
Content: name,
Data: data,
Topics: topicList,
Cities: cityList,
}
a, err := conf.LatestAnnouncement(ctx)
if err != nil {
ctx.Errorf("latest announcement: %v", err)
}
if a != nil {
p.Announcement = a.Message
}
if u := user.Current(ctx); u != nil {
p.User = u
p.LogoutURL, err = user.LogoutURL(ctx, "/")
} else {
p.LoginURL, err = user.LoginURL(ctx, "/")
}
return p, err
}
作者:rcshubhadee
项目:glo
func Admin(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
an := AdminPage{"", ""}
var templ = ""
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
}
c.Debugf("User present %v", u)
an.Admin_email = u.Email
templ = "adminIndex"
url, err := user.LogoutURL(c, "/admin")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
an.LogoutUrl = url
err = templates.ExecuteTemplate(w, templ, an)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
作者:jrandal
项目:cambridge-challenge.appspot.co
func handleLogout(w http.ResponseWriter, r *http.Request) {
c = appengine.NewContext(r)
returnURL := "/"
// parse form
err := r.ParseForm()
if err != nil {
serveError(c, w, err)
return
}
if r.FormValue("continue") != "" {
returnURL = r.FormValue("continue")
}
if useOpenID {
// adjust returnURL to bring us back to a local user login form
laterReturnUrl := returnURL
returnURL = "/Login/?chooseLogin=1&continue=" + http.URLEscape(laterReturnUrl)
}
// redirect to google logout (for OpenID as well, or else we won't be locally logged out)
lourl, err := user.LogoutURL(c, returnURL)
if err != nil {
c.Errorf("handleLogout: error getting LogoutURL")
}
c.Debugf("handleLogout: redirecting to logoutURL=%v", lourl)
http.Redirect(w, r, lourl, http.StatusFound)
return
}
作者:c0nra
项目:crapcha
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
url, _ := user.LogoutURL(c, "/")
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
作者:knights
项目:goslide
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
}
作者:elegio
项目:Scrib
func rootHandler(e env) {
userKey, done := getUser(e)
if done {
return
}
q := datastore.NewQuery("projectTop").Ancestor(userKey).KeysOnly()
e.w.Header().Set("Content-type", "text/html; charset=utf-8")
fmt.Fprintf(e.w, `<ul>`)
for t := q.Run(e.ctx); ; {
key, err := t.Next(nil)
if err == datastore.Done {
break
}
if err != nil {
e.w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(e.w, `</ul>Got an error retrieving projects`)
log.Println("Error retrieving projects", err)
return
}
fmt.Fprintf(e.w, `<li><a href="/%s">%s</a></li>`, key.StringID(), key.StringID())
}
fmt.Fprintf(e.w, `</ul>`)
url, _ := user.LogoutURL(e.ctx, "/")
fmt.Fprintf(e.w, `<a href="%s">Sign out</a>`, url)
}
作者:Softhous
项目:tod
func root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
pageData := TodoPageData{}
if u := user.Current(c); u == nil {
pageData.Username = "Not logged in"
url, err := user.LoginURL(c, r.URL.String())
handleErr(w, err)
pageData.LoginUrl = url
} else {
pageData.Username = u.String()
url, err := user.LogoutURL(c, r.URL.String())
handleErr(w, err)
pageData.LoginUrl = url
}
q := datastore.NewQuery(pageData.Username).Order("-Created").Limit(10)
//make is like a constructor
//[] is a "slice", ref counted, dynamic array
items := make([]TodoItem, 0, 10)
_, err := q.GetAll(c, &items)
pageData.TodoItems = items
templ, err := template.ParseFiles("todo.html")
handleErr(w, err)
err = templ.Execute(w, pageData) // populate html page with data from pageData
handleErr(w, err)
}
作者:nickdufresn
项目:gae-guestbook-bootstrap-g
func root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
panic("User should not be nil")
}
// Ancestor queries, as shown here, are strongly consistent with the High
// Replication Datastore. Queries that span entity groups are eventually
// consistent. If we omitted the .Ancestor from this query there would be
// a slight chance that Greeting that had just been written would not
// show up in a query.
q := datastore.NewQuery("Greeting").Ancestor(guestbookKey(c)).Order("-Date").Limit(10)
greetings := make([]Greeting, 0, 10)
if _, err := q.GetAll(c, &greetings); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url, _ := user.LogoutURL(c, "/")
//fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
page := GreetingPage{
User: u,
SignOutURL: url,
Greetings: greetings,
}
if err := guestbookTemplate.Execute(w, page); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
作者:icz
项目:iczagp
// 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
}
作者:SII-Nante
项目:callForPaperG
/**
* get admin user info and connect/disconnect link
* @param {[type]} w http.ResponseWriter [description]
* @param {[type]} r *http.Request [description]
* @return {[type]} [description]
*/
func getCurrentUser(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
connected := (u != nil)
if !connected {
uri, _ := user.LoginURL(c, "/")
currentUser := &CurrentUser{
Connected: connected,
Admin: false,
Config: true,
Uri: uri,
}
json.NewEncoder(w).Encode(currentUser)
} else {
uri, _ := user.LogoutURL(c, "/")
currentUser := &CurrentUser{
Connected: connected,
Admin: u.Admin,
Config: true,
Uri: uri,
}
json.NewEncoder(w).Encode(currentUser)
}
return
}
作者:danielfirema
项目:isum
func LogoutURL(c appengine.Context, r *http.Request) (string, error) {
url, err := user.LogoutURL(c, r.URL.String())
if err != nil {
return "", err
}
return url, nil
}
作者:runningwil
项目:votasti
func headerLoggedIn(w http.ResponseWriter, r *http.Request, c appengine.Context, u *user.User) {
url, err := user.LogoutURL(c, "/")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)<br>`, u.Email, url)
}
}
作者:wolfgangihlof
项目:bongoap
func logout(res http.ResponseWriter, req *http.Request) {
c := appengine.NewContext(req)
logout_url, err := user.LogoutURL(c, "/")
if error_check(res, err) {
return
}
http.Redirect(res, req, logout_url, http.StatusTemporaryRedirect)
}