當前位置: 首頁>>代碼示例>>Golang>>正文


Golang bitmonster.Context類代碼示例

本文整理匯總了Golang中github.com/desertbit/bitmonster.Context的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: changeUsername

func changeUsername(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		ID       string `json:"id"`
		Username string `json:"username"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Obtain the user.
	user, err := GetUser(data.ID)
	if err != nil {
		return err
	}

	// Change the user's username.
	err = ChangeUsername(user, data.Username)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:26,代碼來源:module.go

示例2: deleteUser

func deleteUser(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		ID string `json:"id"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Obtain the user.
	user, err := GetUser(data.ID)
	if err != nil {
		return err
	}

	// Delete the user.
	err = DeleteUser(user)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:25,代碼來源:module.go

示例3: changePassword

func changePassword(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		ID       string `json:"id"`
		Password string `json:"password"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Obtain the user.
	user, err := GetUser(data.ID)
	if err != nil {
		return err
	}

	// Change the user's password.
	err = user.ChangePassword(data.Password)
	if err != nil {
		return err
	}

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:32,代碼來源:module.go

示例4: getUsers

func getUsers(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		Groups []string `json:"groups"`
	}{}

	err := c.Decode(&data)
	// Skip if no context data is available.
	if err != nil && err != bitmonster.ErrNoContextData {
		return err
	}

	// Prepare the data.
	data.Groups = utils.RemoveDuplicateStrings(data.Groups)

	// Obtain the users.
	users, err := GetUsers(data.Groups...)
	if err != nil {
		return err
	}

	// Set the users as return data.
	c.Data(users)

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:26,代碼來源:module.go

示例5: clearAuthSessions

func clearAuthSessions(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		ID string `json:"id"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Obtain the user
	user, err := GetUser(data.ID)
	if err != nil {
		return err
	}

	// Clear the authenticated sessions.
	user.ClearAuthSessions()

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:28,代碼來源:module.go

示例6: Hook

func (h *isAuthHook) Hook(c *bitmonster.Context) error {
	if !IsAuth(c.Socket()) {
		return fmt.Errorf("socket session is not authorized")
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:7,代碼來源:hooks.go

示例7: getGroups

func getGroups(c *bitmonster.Context) error {
	// Obtain all groups.
	groups := Groups()

	// Set the groups slice as return data.
	c.Data(groups)

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:9,代碼來源:module.go

示例8: getCurrentUser

func getCurrentUser(c *bitmonster.Context) error {
	// Obtain the current user.
	user, err := CurrentUser(c.Socket())
	if err != nil {
		return err
	}

	// Set the user as return data.
	c.Data(user)

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:12,代碼來源:module.go

示例9: clearAuthSessionsOfCurrentUser

func clearAuthSessionsOfCurrentUser(c *bitmonster.Context) error {
	// Obtain the current user.
	user, err := CurrentUser(c.Socket())
	if err != nil {
		return err
	}

	// Clear the authenticated sessions.
	user.ClearAuthSessions()

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:18,代碼來源:module.go

示例10: logout

func logout(c *bitmonster.Context) error {
	// Get the socket.
	s := c.Socket()

	// Get the current authenticated user.
	user, err := CurrentUser(s)
	if err != nil {
		if err == ErrNotAuth {
			// Not authenticated.
			return nil
		}
		return err
	}

	// Debug log.
	log.L.WithFields(logrus.Fields{
		"remoteAddress": s.RemoteAddr(),
		"user":          user.Username,
		"userID":        user.ID,
	}).Debugf("auth: logout")

	// Get the current socket value.
	av := getAuthSocketValue(s)
	if av == nil {
		return fmt.Errorf("failed to obtain auth socket value")
	}

	// Reset the socket authentication values.
	resetAuthSocketValue(s)

	// Remove the authenticated session specified by the key.
	if user.AuthSessions != nil {
		delete(user.AuthSessions, av.authSessionKey)
	}

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:43,代碼來源:module.go

示例11: editUser

func editUser(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		ID string `json:"id"`

		// All these values replace the values of the user:
		Name    string   `json:"name"`
		Email   string   `json:"email"`
		Enabled bool     `json:"enabled"`
		Groups  []string `json:"groups"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Prepare the data.
	data.Groups = utils.RemoveDuplicateStrings(data.Groups)

	// Obtain the user.
	user, err := GetUser(data.ID)
	if err != nil {
		return err
	}

	// Update the user fields.
	user.Name = data.Name
	user.Email = data.Email
	user.Enabled = data.Enabled
	user.Groups = data.Groups

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	// Set the updated user as return data.
	c.Data(user)

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:43,代碼來源:module.go

示例12: addUser

func addUser(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		Username string `json:"username"`
		Name     string `json:"name"`
		EMail    string `json:"email"`
		Password string `json:"password"`

		Enabled bool     `json:"enabled"`
		Groups  []string `json:"groups"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Prepare the data.
	data.Groups = utils.RemoveDuplicateStrings(data.Groups)

	// Create a new user value.
	// Fields are validated by the function.
	user, err := NewUser(data.Username, data.Name, data.EMail, data.Password)
	if err != nil {
		return err
	}

	// Set the additinal data fields.
	user.Enabled = data.Enabled
	user.Groups = data.Groups

	// Add the user to the database.
	err = AddUser(user)
	if err != nil {
		return err
	}

	// Set the newly created user as return data.
	c.Data(user)

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:42,代碼來源:module.go

示例13: editCurrentUser

func editCurrentUser(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		// All these values replace the values of the user:
		Name  string `json:"name"`
		Email string `json:"email"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Obtain the current user.
	user, err := CurrentUser(c.Socket())
	if err != nil {
		return err
	}

	// Update the user fields.
	user.Name = data.Name
	user.Email = data.Email

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	// Set the updated user as return data.
	c.Data(user)

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:34,代碼來源:module.go

示例14: getUsers

func getUsers(c *bitmonster.Context) error {
	println("getUsers")

	v := struct {
		Foo string
		Bar string
	}{}

	err := c.Decode(&v)
	if err != nil {
		return err
	}

	fmt.Printf("%+v\n", v)

	// Get the event.
	e, err := c.Module().Event("onNew")
	if err != nil {
		return err
	}

	// Trigger the event.
	err = e.Trigger(v)
	if err != nil {
		return err
	}

	// Only this socket.
	err = e.TriggerSocket(c.Socket(), v)
	if err != nil {
		return err
	}

	c.Data(&v)
	c.Error("error message")

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:38,代碼來源:sample.go

示例15: changePasswordOfCurrentUser

func changePasswordOfCurrentUser(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		OldPassword string `json:"oldPassword"`
		NewPassword string `json:"newPassword"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Obtain the current user.
	user, err := CurrentUser(c.Socket())
	if err != nil {
		return err
	}

	// Check if the old password is valid.
	if match := user.ComparePasswords(data.OldPassword); !match {
		c.Error("wrong_password")
		return nil
	}

	// Change the user's password.
	err = user.ChangePassword(data.NewPassword)
	if err != nil {
		return err
	}

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:desertbit,項目名稱:bitmonster,代碼行數:38,代碼來源:module.go


注:本文中的github.com/desertbit/bitmonster.Context類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。