本文整理汇总了Golang中github.com/verifiedninja/webapp/shared/session.Instance函数的典型用法代码示例。如果您正苦于以下问题:Golang Instance函数的具体用法?Golang Instance怎么用?Golang Instance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Instance函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: EmailVerificationGET
func EmailVerificationGET(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
var params httprouter.Params
params = context.Get(r, "params").(httprouter.Params)
token := params.ByName("token")
result, err := model.UserEmailVerified(token)
// Will only error if there is a problem with the query
if err != nil {
//log.Println(err)
sess.AddFlash(view.Flash{"Email verification link is not valid. Please check the latest email we sent you.", view.FlashError})
sess.Save(r, w)
} else if result {
sess.AddFlash(view.Flash{"Email verified. You may login now.", view.FlashSuccess})
sess.Save(r, w)
} else {
sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
sess.Save(r, w)
}
http.Redirect(w, r, "/", http.StatusFound)
}
示例2: deletePhoto
func deletePhoto(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
var params httprouter.Params
params = context.Get(r, "params").(httprouter.Params)
//userid := params.ByName("userid")
//userid := uint64(site_idInt)
userid := uint64(sess.Values["id"].(uint32))
picid := params.ByName("picid")
err := model.PhotoDelete(userid, picid)
if err != nil {
log.Println(err)
sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
sess.Save(r, w)
} else {
/*err = os.Remove(photoPath + fmt.Sprintf("%v", userid) + "/" + picid + ".jpg")
if err != nil {
log.Println(err)
}*/
sess.AddFlash(view.Flash{"Photo removed!", view.FlashSuccess})
sess.Save(r, w)
}
}
示例3: Index
// Displays the default home page
func Index(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
// If the user is logged in
if sess.Values["id"] != nil {
// Get the current user role
currentUID, _ := CurrentUserId(r)
role, err := model.RoleByUserId(int64(currentUID))
if err != nil {
log.Println(err)
Error500(w, r)
return
}
if role.Level_id == model.Role_level_User {
http.Redirect(w, r, "/profile", http.StatusFound)
return
} else {
http.Redirect(w, r, "/admin", http.StatusFound)
return
}
} else {
// Display the view
v := view.New(r)
v.Name = "anon_home"
v.Render(w)
}
}
示例4: New
// New returns a new view
func New(req *http.Request) *View {
v := &View{}
v.Vars = make(map[string]interface{})
v.Vars["AuthLevel"] = "anon"
v.BaseURI = viewInfo.BaseURI
v.Extension = viewInfo.Extension
v.Folder = viewInfo.Folder
v.Name = viewInfo.Name
// Make sure BaseURI is available in the templates
v.Vars["BaseURI"] = v.BaseURI
// This is required for the view to access the request
v.request = req
// Get session
session := session.Instance(v.request)
// Set the AuthLevel to auth if the user is logged in
if session.Values["id"] != nil {
v.Vars["AuthLevel"] = "auth"
v.Vars["UserFirstName"] = session.Values["first_name"]
v.Vars["UserIdGA"] = session.Values["id"]
} else {
v.Vars["UserIdGA"] = 0
}
return v
}
示例5: PhotoUploadGET
func PhotoUploadGET(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
// Get the user photos
photos, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
if err != nil {
log.Println(err)
}
verified := false
for _, v := range photos {
if v.Status_id == 1 {
verified = true
break
}
}
// Only allow access to this page if verified
if verified {
// Display the view
v := view.New(r)
v.Name = "user_upload"
v.Render(w)
} else {
Error404(w, r)
}
}
示例6: PhotoDownloadGET
func PhotoDownloadGET(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
var params = context.Get(r, "params").(httprouter.Params)
//userid := params.ByName("userid")
pic_id := params.ByName("picid")
//user_id, _ := strconv.Atoi(userid)
user_id := uint64(sess.Values["id"].(uint32))
userid := strconv.Itoa(int(user_id))
if allowed, mark, err := photoAccessAllowed(r, user_id, pic_id); allowed {
buffer, err := renderImage(w, r, userid, pic_id, mark)
if err != nil {
log.Println(err)
Error500(w, r)
return
}
// Force download
w.Header().Set("Content-Disposition", `attachment; filename="`+pic_id+`"`)
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
http.ServeContent(w, r, pic_id, time.Now(), bytes.NewReader(buffer.Bytes()))
} else if err != sql.ErrNoRows {
log.Println(err)
Error500(w, r)
return
} else {
//log.Println("User does not have access to the photo.")
Error401(w, r)
return
}
}
示例7: CurrentUserId
// Returns the user_id in uint32 and whether the a user is logged in
func CurrentUserId(r *http.Request) (uint32, bool) {
// Get session
sess := session.Instance(r)
if sess.Values["id"] == nil {
return 0, false
}
return sess.Values["id"].(uint32), true
}
示例8: ContactPOST
func ContactPOST(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
// Validate with required fields
if validate, missingField := view.Validate(r, []string{"email", "fullname", "message"}); !validate {
sess.AddFlash(view.Flash{"Field missing: " + missingField, view.FlashError})
sess.Save(r, w)
ContactGET(w, r)
return
}
// Validate with Google reCAPTCHA
if !recaptcha.Verified(r) {
sess.AddFlash(view.Flash{"reCAPTCHA invalid!", view.FlashError})
sess.Save(r, w)
ContactGET(w, r)
return
}
// Form values
email := r.FormValue("email")
name := r.FormValue("fullname")
message := r.FormValue("message")
ip, err := model.GetRemoteIP(r)
if err != nil {
log.Println(err)
}
user := "Guest"
if sess.Values["id"] != nil {
user = fmt.Sprintf("Registered (%v)", sess.Values["id"])
}
// Email the hash to the user
err = emailer.SendEmail(emailer.ReadConfig().From, "Contact Submission for Verified.ninja", "From: "+
name+" <"+email+">\nUser: "+user+"\nIP: "+ip+"\nMessage: "+message)
if err != nil {
log.Println(err)
sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
sess.Save(r, w)
ContactGET(w, r)
return
}
// Post successful
sess.AddFlash(view.Flash{"Thanks for the message! We'll get back to you in a bit.", view.FlashSuccess})
sess.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
return
}
示例9: Logout
func Logout(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
// If user is authenticated
if sess.Values["id"] != nil {
clearSessionVariables(sess)
sess.AddFlash(view.Flash{"Goodbye!", view.FlashNotice})
sess.Save(r, w)
}
http.Redirect(w, r, "/", http.StatusFound)
}
示例10: DisallowAnon
// DisallowAnon does not allow anonymous users to access the page
func DisallowAnon(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
// If user is not authenticated, don't allow them to access the page
if sess.Values["id"] == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
h.ServeHTTP(w, r)
})
}
示例11: UserSiteGET
func UserSiteGET(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
// Does the user have a verified photo
verified := isVerified(r)
// Only allow access to this page if verified
if verified {
// Get database result
sites, err := model.SiteList()
if err != nil {
log.Println(err)
Error500(w, r)
return
}
user_id := uint64(sess.Values["id"].(uint32))
usernames, err := model.UsernamesByUserId(user_id)
if err != nil {
log.Println(err)
Error500(w, r)
return
}
// err == sql.ErrNoRows
// Display the view
v := view.New(r)
v.Name = "user_site"
v.Vars["first_name"] = sess.Values["first_name"]
v.Vars["sites"] = sites
// Copy the usernames into a map so they can be used in the form inputs
data := make(map[uint32]string)
for _, u := range usernames {
data[u.Site_id] = u.Name
}
v.Vars["data"] = data
v.Render(w)
} else {
Error404(w, r)
}
}
示例12: APIRegisterChromeGET
func APIRegisterChromeGET(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
// If the user is logged in
if sess.Values["id"] == nil {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"error": "Authentication required"}`))
return
}
user_id := uint64(sess.Values["id"].(uint32))
ci := ChromeInfo{}
auth, err := model.ApiAuthenticationByUserId(user_id)
// If there is no record yet, create one
if err == sql.ErrNoRows {
// Create values
ci.Userkey = random.Generate(32)
ci.Token = random.Generate(32)
err := model.ApiAuthenticationCreate(user_id, ci.Userkey, ci.Token)
if err != nil {
log.Println(err)
Error500(w, r)
return
}
} else if err != nil {
log.Println(err)
Error500(w, r)
return
} else {
ci.Userkey = auth.Userkey
ci.Token = auth.Token
}
js, err := json.Marshal(ci)
if err != nil {
log.Println(err)
Error500(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
示例13: SendFlashes
func (v *View) SendFlashes(w http.ResponseWriter) {
// Get session
sess := session.Instance(v.request)
flashes := peekFlashes(w, v.request)
sess.Save(v.request, w)
js, err := json.Marshal(flashes)
if err != nil {
http.Error(w, "JSON Error: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
示例14: isVerified
func isVerified(r *http.Request) bool {
// Get session
sess := session.Instance(r)
// Get the user photos
photos, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
if err != nil {
log.Println(err)
}
verified := false
for _, v := range photos {
if v.Status_id == 1 {
verified = true
break
}
}
return verified
}
示例15: UserEmailGET
func UserEmailGET(w http.ResponseWriter, r *http.Request) {
// Get session
sess := session.Instance(r)
user_id := int64(sess.Values["id"].(uint32))
if !isVerifiedEmail(r, user_id) {
sess.AddFlash(view.Flash{"You can't change you email again until you verify your current email.", view.FlashError})
sess.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
// Display the view
v := view.New(r)
v.Name = "user_email"
v.Vars["emailold"] = sess.Values["email"]
// Refill any form fields
view.Repopulate([]string{"email"}, r.Form, v.Vars)
v.Render(w)
}