本文整理汇总了Golang中github.com/xyproto/pinterface.IUserState.HasUser方法的典型用法代码示例。如果您正苦于以下问题:Golang IUserState.HasUser方法的具体用法?Golang IUserState.HasUser怎么用?Golang IUserState.HasUser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/xyproto/pinterface.IUserState
的用法示例。
在下文中一共展示了IUserState.HasUser方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GenerateLogoutCurrentUser
// Log out a user by changing the loggedin value
func GenerateLogoutCurrentUser(state pinterface.IUserState) SimpleContextHandle {
return func(ctx *web.Context) string {
username := state.Username(ctx.Request)
if username == "" {
return MessageOKback("Logout", "No user to log out")
}
if !state.HasUser(username) {
return MessageOKback("Logout", "user "+username+" does not exist, could not log out")
}
// Log out the user by changing the database, the cookie can stay
state.SetLoggedOut(username)
// Redirect
//ctx.SetHeader("Refresh", "0; url=/login", true)
return MessageOKurl("Logout", username+" is now logged out. Hope to see you soon!", "/login")
}
}
示例2: GenerateRemoveUser
// TODO: Undo for removing users
// Remove a user
func GenerateRemoveUser(state pinterface.IUserState) WebHandle {
return func(ctx *web.Context, username string) string {
if !state.AdminRights(ctx.Request) {
return MessageOKback("Remove user", "Not logged in as Administrator")
}
if username == "" {
return MessageOKback("Remove user", "Can't remove blank user")
}
if !state.HasUser(username) {
return MessageOKback("Remove user", username+" doesn't exists, could not remove")
}
// Remove the user
state.RemoveUser(username)
return MessageOKurl("Remove user", "OK, removed "+username, "/admin")
}
}
示例3: GenerateStatusUser
func GenerateStatusUser(state pinterface.IUserState) WebHandle {
return func(ctx *web.Context, username string) string {
if username == "" {
return MessageOKback("Status", "No username given")
}
if !state.HasUser(username) {
return MessageOKback("Status", username+" does not exist")
}
loggedinStatus := "not logged in"
if state.IsLoggedIn(username) {
loggedinStatus = "logged in"
}
confirmStatus := "email has not been confirmed"
if state.IsConfirmed(username) {
confirmStatus = "email has been confirmed"
}
return MessageOKback("Status", username+" is "+loggedinStatus+" and "+confirmStatus)
}
}
示例4: GenerateStatusCurrentUser
func GenerateStatusCurrentUser(state pinterface.IUserState) SimpleContextHandle {
return func(ctx *web.Context) string {
if !state.AdminRights(ctx.Request) {
return MessageOKback("Status", "Not logged in as Administrator")
}
username := state.Username(ctx.Request)
if username == "" {
return MessageOKback("Current user status", "No user logged in")
}
hasUser := state.HasUser(username)
if !hasUser {
return MessageOKback("Current user status", username+" does not exist")
}
if !(state.IsLoggedIn(username)) {
return MessageOKback("Current user status", "User "+username+" is not logged in")
}
return MessageOKback("Current user status", "User "+username+" is logged in")
}
}
示例5: GenerateConfirmUser
// Create a user by adding the username to the list of usernames
func GenerateConfirmUser(state pinterface.IUserState) WebHandle {
return func(ctx *web.Context, val string) string {
confirmationCode := val
unconfirmedUsernames, err := state.AllUnconfirmedUsernames()
if err != nil {
return MessageOKurl("Confirmation", "All users are confirmed already.", "/register")
}
// Find the username by looking up the confirmationCode on unconfirmed users
username := ""
for _, aUsername := range unconfirmedUsernames {
aConfirmationCode, err := state.ConfirmationCode(aUsername)
if err != nil {
// If the confirmation code can not be found, just skip this one
continue
}
if confirmationCode == aConfirmationCode {
// Found the right user
username = aUsername
break
}
}
// Check that the user is there
if username == "" {
// Say "no longer" because we don't care about people that just try random confirmation links
return MessageOKurl("Confirmation", "The confirmation link is no longer valid.", "/register")
}
hasUser := state.HasUser(username)
if !hasUser {
return MessageOKurl("Confirmation", "The user you wish to confirm does not exist anymore.", "/register")
}
// Remove from the list of unconfirmed usernames
state.RemoveUnconfirmed(username)
// Mark user as confirmed
state.MarkConfirmed(username)
return MessageOKurl("Confirmation", "Thank you "+username+", you can now log in.", "/login")
}
}
示例6: GenerateLoginUser
// Log in a user by changing the loggedin value
func GenerateLoginUser(state pinterface.IUserState) WebHandle {
return func(ctx *web.Context, val string) string {
// Fetch password from ctx
password, found := ctx.Params["password"]
if !found {
return MessageOKback("Login", "Can't log in without a password.")
}
username := val
if username == "" {
return MessageOKback("Login", "Can't log in with a blank username.")
}
if !state.HasUser(username) {
return MessageOKback("Login", "User "+username+" does not exist, could not log in.")
}
if !state.IsConfirmed(username) {
return MessageOKback("Login", "The email for "+username+" has not been confirmed, check your email and follow the link.")
}
if !state.CorrectPassword(username, password) {
return MessageOKback("Login", "Wrong password.")
}
// Log in the user by changing the database and setting a secure cookie
state.SetLoggedIn(username)
// Also store the username in the browser
state.SetUsernameCookie(ctx.ResponseWriter, username)
// TODO: Use a welcoming messageOK where the user can see when he/she last logged in and from which host
if username == "admin" {
ctx.SetHeader("Refresh", "0; url=/admin", true)
} else {
// TODO: Redirect to the page the user was at before logging in
ctx.SetHeader("Refresh", "0; url=/", true)
}
return ""
}
}
示例7: GenerateToggleAdmin
func GenerateToggleAdmin(state pinterface.IUserState) WebHandle {
return func(ctx *web.Context, username string) string {
if !state.AdminRights(ctx.Request) {
return MessageOKback("Admin toggle", "Not logged in as Administrator")
}
if username == "" {
return MessageOKback("Admin toggle", "Can't set toggle empty username")
}
if !state.HasUser(username) {
return MessageOKback("Admin toggle", "Can't toggle non-existing user")
}
// A special case
if username == "admin" {
return MessageOKback("Admin toggle", "Can't remove admin rights from the admin user")
}
if !state.IsAdmin(username) {
state.SetAdminStatus(username)
return MessageOKurl("Admin toggle", "OK, "+username+" is now an admin", "/admin")
}
state.RemoveAdminStatus(username)
return MessageOKurl("Admin toggle", "OK, "+username+" is now a regular user", "/admin")
}
}
示例8: exportUserstate
// Make functions related to users and permissions available to Lua scripts
func exportUserstate(w http.ResponseWriter, req *http.Request, L *lua.LState, userstate pinterface.IUserState) {
// Check if the current user has "user rights", returns bool
// Takes no arguments
L.SetGlobal("UserRights", L.NewFunction(func(L *lua.LState) int {
L.Push(lua.LBool(userstate.UserRights(req)))
return 1 // number of results
}))
// Check if the given username exists, returns bool
// Takes a username
L.SetGlobal("HasUser", L.NewFunction(func(L *lua.LState) int {
username := L.ToString(1)
L.Push(lua.LBool(userstate.HasUser(username)))
return 1 // number of results
}))
// Get the value from the given boolean field, returns bool
// Takes a username and fieldname
L.SetGlobal("BooleanField", L.NewFunction(func(L *lua.LState) int {
username := L.ToString(1)
fieldname := L.ToString(2)
L.Push(lua.LBool(userstate.BooleanField(username, fieldname)))
return 1 // number of results
}))
// Save a value as a boolean field, returns nothing
// Takes a username, fieldname and boolean value
L.SetGlobal("SetBooleanField", L.NewFunction(func(L *lua.LState) int {
username := L.ToString(1)
fieldname := L.ToString(2)
value := L.ToBool(3)
userstate.SetBooleanField(username, fieldname, value)
return 0 // number of results
}))
// Check if a given username is confirmed, returns a bool
// Takes a username
L.SetGlobal("IsConfirmed", L.NewFunction(func(L *lua.LState) int {
username := L.ToString(1)
L.Push(lua.LBool(userstate.IsConfirmed(username)))
return 1 // number of results
}))
// Check if a given username is logged in, returns a bool
// Takes a username
L.SetGlobal("IsLoggedIn", L.NewFunction(func(L *lua.LState) int {
username := L.ToString(1)
L.Push(lua.LBool(userstate.IsLoggedIn(username)))
return 1 // number of results
}))
// Check if the current user has "admin rights", returns a bool
// Takes no arguments.
L.SetGlobal("AdminRights", L.NewFunction(func(L *lua.LState) int {
L.Push(lua.LBool(userstate.AdminRights(req)))
return 1 // number of results
}))
// Check if a given username is an admin, returns a bool
// Takes a username
L.SetGlobal("IsAdmin", L.NewFunction(func(L *lua.LState) int {
username := L.ToString(1)
L.Push(lua.LBool(userstate.IsAdmin(username)))
return 1 // number of results
}))
// Get the username stored in a cookie, or an empty string
// Takes no arguments
L.SetGlobal("UsernameCookie", L.NewFunction(func(L *lua.LState) int {
username, err := userstate.UsernameCookie(req)
var result lua.LString
if err != nil {
result = lua.LString("")
} else {
result = lua.LString(username)
}
L.Push(result)
return 1 // number of results
}))
// Store the username in a cookie, returns true if successful
// Takes a username
L.SetGlobal("SetUsernameCookie", L.NewFunction(func(L *lua.LState) int {
username := L.ToString(1)
L.Push(lua.LBool(nil == userstate.SetUsernameCookie(w, username)))
return 1 // number of results
}))
// Clear the user cookie. The result depends on the browser.
L.SetGlobal("ClearCookie", L.NewFunction(func(L *lua.LState) int {
userstate.ClearCookie(w)
return 0 // number of results
}))
// Get the username stored in a cookie, or an empty string
// Takes no arguments
L.SetGlobal("AllUsernames", L.NewFunction(func(L *lua.LState) int {
usernames, err := userstate.AllUsernames()
var table *lua.LTable
if err != nil {
table = L.NewTable()
} else {
table = strings2table(L, usernames)
}
L.Push(table)
return 1 // number of results
}))
// Get the email for a given username, or an empty string
// Takes a username
L.SetGlobal("Email", L.NewFunction(func(L *lua.LState) int {
//.........这里部分代码省略.........
示例9: GenerateRegisterUser
// Register a new user, site is ie. "archlinux.no"
func GenerateRegisterUser(state pinterface.IUserState, site string) WebHandle {
return func(ctx *web.Context, val string) string {
// Password checks
password1, found := ctx.Params["password1"]
if password1 == "" || !found {
return MessageOKback("Register", "Can't register without a password.")
}
password2, found := ctx.Params["password2"]
if password2 == "" || !found {
return MessageOKback("Register", "Please confirm the password by typing it in twice.")
}
if password1 != password2 {
return MessageOKback("Register", "The password and confirmation password must be equal.")
}
// Email checks
email, found := ctx.Params["email"]
if !found {
return MessageOKback("Register", "Can't register without an email address.")
}
// must have @ and ., but no " "
if !strings.Contains(email, "@") || !strings.Contains(email, ".") || strings.Contains(email, " ") {
return MessageOKback("Register", "Please use a valid email address.")
}
if email != CleanUserInput(email) {
return MessageOKback("Register", "The sanitized email differs from the given email.")
}
// Username checks
username := val
if username == "" {
return MessageOKback("Register", "Can't register without a username.")
}
if state.HasUser(username) {
return MessageOKback("Register", "That user already exists, try another username.")
}
// Only some letters are allowed in the username
err := permissions.ValidUsernamePassword(username, password1)
if err != nil {
return MessageOKback("Register", err.Error())
}
adminuser := false
// A special case
if username == "admin" {
// The first user to register with the username "admin" becomes the administrator
adminuser = true
}
// Register the user
state.AddUser(username, password1, email)
// Mark user as administrator if that is the case
if adminuser {
// Set admin status
state.SetAdminStatus(username)
}
confirmationCode, err := state.GenerateUniqueConfirmationCode()
if err != nil {
panic(err.Error())
}
// If registering the admin user (first user on the system), don't send a confirmation email, just register it
if adminuser {
// Mark user as confirmed
state.MarkConfirmed(username)
// Redirect
return MessageOKurl("Registration complete", "Thanks for registering, the admin user has been created.", "/login")
}
// Send confirmation email
ConfirmationEmail(site, "https://"+site+"/confirm/"+confirmationCode, username, email)
// Register the need to be confirmed
state.AddUnconfirmed(username, confirmationCode)
// Redirect
return MessageOKurl("Registration complete", "Thanks for registering, the confirmation e-mail has been sent.", "/login")
}
}