Ejemplo n.º 1
0
func (Admin) AdminChangeRole(server *wsevent.Server, so *wsevent.Client, data []byte) []byte {
	reqerr := chelpers.FilterRequest(so, 0, true)

	if reqerr != nil {
		return reqerr.Encode()
	}
	var args struct {
		Steamid *string `json:"steamid"`
		Role    *string `json:"role"`
	}

	err := chelpers.GetParams(data, &args)
	if err != nil {
		return helpers.NewTPErrorFromError(err).Encode()
	}

	role, ok := helpers.RoleMap[*args.Role]
	if !ok || role == helpers.RoleAdmin {
		return helpers.NewTPError("Invalid role parameter", 0).Encode()
	}

	otherPlayer, err := models.GetPlayerBySteamId(*args.Steamid)
	if err != nil {
		return helpers.NewTPError("Player not found.", 0).Encode()
	}

	currPlayer, _ := chelpers.GetPlayerSocket(so.Id())

	models.LogAdminAction(currPlayer.ID, helpers.ActionChangeRole, otherPlayer.ID)

	// actual change happens
	otherPlayer.Role = role
	db.DB.Save(&otherPlayer)

	// rewrite session data. THIS WON'T WRITE A COOKIE SO IT ONLY WORKS WITH
	// STORES THAT STORE DATA IN COOKIES (AND NOT ONLY SESSION ID).
	session, sesserr := chelpers.GetSessionHTTP(so.Request())
	if sesserr == nil {
		session.Values["role"] = role
		session.Save(so.Request(), FakeResponseWriter{})
	}

	return chelpers.EmptySuccessJS
}
Ejemplo n.º 2
0
Archivo: admin.go Proyecto: N1xx1/Helen
func AdminChangeRole(so socketio.Socket) func(string) string {
	return chelpers.FilterRequest(so, adminChangeRoleFilter,

		func(params map[string]interface{}) string {
			roleString := params["role"].(string)
			steamid := params["steamid"].(string)
			role, ok := helpers.RoleMap[roleString]
			if !ok || role == helpers.RoleAdmin {
				bytes, _ := chelpers.BuildFailureJSON("Invalid role parameter", 0).Encode()
				return string(bytes)
			}

			otherPlayer, err := models.GetPlayerBySteamId(steamid)
			if err != nil {
				bytes, _ := chelpers.BuildFailureJSON("Player not found.", 0).Encode()
				return string(bytes)
			}

			currPlayer, _ := chelpers.GetPlayerSocket(so.Id())

			models.LogAdminAction(currPlayer.ID, helpers.ActionChangeRole, otherPlayer.ID)

			// actual change happens
			otherPlayer.Role = role
			db.DB.Save(&otherPlayer)

			// rewrite session data. THiS WON'T WRITE A COOKIE SO IT ONLY WORKS WITH
			// STORES THAT STORE DATA IN COOKIES (AND NOT ONLY SESSION ID).
			session, sesserr := chelpers.GetSessionHTTP(so.Request())
			if sesserr == nil {
				session.Values["role"] = role
				session.Save(so.Request(), FakeResponseWriter{})
			}

			return chelpers.BuildEmptySuccessString()
		})
}