当前位置: 首页>>代码示例>>Golang>>正文


Golang utils.GetCtxUsername函数代码示例

本文整理汇总了Golang中github.com/ovh/tat/utils.GetCtxUsername函数的典型用法代码示例。如果您正苦于以下问题:Golang GetCtxUsername函数的具体用法?Golang GetCtxUsername怎么用?Golang GetCtxUsername使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了GetCtxUsername函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: checkDMTopic

func (m *MessagesController) checkDMTopic(ctx *gin.Context, topicName string) (models.Topic, string, error) {
	var topic = models.Topic{}

	topicParentName := "/Private/" + utils.GetCtxUsername(ctx) + "/DM"
	if !strings.HasPrefix(topicName, topicParentName+"/") {
		log.Errorf("wrong topic name for DM:" + topicName)
		return topic, "", errors.New("Wrong tpic name for DM:" + topicName)
	}

	// /Private/usernameFrom/DM/usernameTO
	part := strings.Split(topicName, "/")
	if len(part) != 5 {
		log.Errorf("wrong topic name for DM")
		return topic, "", errors.New("Wrong topic name for DM:" + topicName)
	}

	var userFrom = models.User{}
	err := userFrom.FindByUsername(utils.GetCtxUsername(ctx))
	if err != nil {
		return topic, "", errors.New("Error while fetching user.")
	}
	var userTo = models.User{}
	usernameTo := part[4]
	err = userTo.FindByUsername(usernameTo)
	if err != nil {
		return topic, "", errors.New("Error while fetching user.")
	}

	err = m.checkTopicParentDM(userFrom)
	if err != nil {
		return topic, "", errors.New(err.Error())
	}

	err = m.checkTopicParentDM(userTo)
	if err != nil {
		return topic, "", errors.New(err.Error())
	}

	topic, err = m.insertTopicDM(userFrom, userTo)
	if err != nil {
		return topic, "", errors.New(err.Error())
	}

	_, err = m.insertTopicDM(userTo, userFrom)
	if err != nil {
		return topic, "", errors.New(err.Error())
	}

	topicCriteria := topicName + "," + "/Private/" + usernameTo + "/DM/" + userFrom.Username
	return topic, topicCriteria, nil
}
开发者ID:vmalguy,项目名称:tat,代码行数:51,代码来源:messages.go

示例2: inverseIfDMTopic

func (m *MessagesController) inverseIfDMTopic(ctx *gin.Context, topicName string) string {
	if !strings.HasPrefix(topicName, "/Private/") {
		return topicName
	}
	if !strings.HasSuffix(topicName, "/DM/"+utils.GetCtxUsername(ctx)) {
		return topicName
	}

	// /Private/usernameFrom/DM/usernameTO
	part := strings.Split(topicName, "/")
	if len(part) != 5 {
		return topicName
	}
	return "/Private/" + utils.GetCtxUsername(ctx) + "/DM/" + part[2]
}
开发者ID:vmalguy,项目名称:tat,代码行数:15,代码来源:messages.go

示例3: Contacts

// Contacts retrieves contacts presences since n seconds
func (*UsersController) Contacts(ctx *gin.Context) {
	sinceSeconds, err := GetParam(ctx, "sinceSeconds")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while getting seconds parameter"})
		return
	}
	seconds, err := strconv.ParseInt(sinceSeconds, 10, 64)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid since parameter : must be an interger"})
		return
	}

	var user = models.User{}
	err = user.FindByUsername(utils.GetCtxUsername(ctx))
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, errors.New("Error while fetching user"))
		return
	}
	criteria := models.PresenceCriteria{}
	for _, contact := range user.Contacts {
		criteria.Username = criteria.Username + "," + contact.Username
	}
	criteria.DateMinPresence = strconv.FormatInt(time.Now().Unix()-seconds, 10)
	count, presences, _ := models.ListPresences(&criteria)

	out := &contactsJSON{
		Contacts:               user.Contacts,
		CountContactsPresences: count,
		ContactsPresences:      &presences,
	}
	ctx.JSON(http.StatusOK, out)
}
开发者ID:vmalguy,项目名称:tat,代码行数:33,代码来源:users.go

示例4: Convert

// Convert a "normal" user to a "system" user
func (*UsersController) Convert(ctx *gin.Context) {
	var convertJSON convertUserJSON
	ctx.Bind(&convertJSON)

	if !strings.HasPrefix(convertJSON.Username, "tat.system") {
		AbortWithReturnError(ctx, http.StatusBadRequest, fmt.Errorf("Username does not begin with tat.system (%s), it's not possible to convert this user", convertJSON.Username))
		return
	}

	var userToConvert = models.User{}
	err := userToConvert.FindByUsername(convertJSON.Username)
	if err != nil {
		AbortWithReturnError(ctx, http.StatusBadRequest, fmt.Errorf("user with username %s does not exist", convertJSON.Username))
		return
	}

	if userToConvert.IsSystem {
		AbortWithReturnError(ctx, http.StatusBadRequest, fmt.Errorf("user with username %s is already a system user", convertJSON.Username))
		return
	}

	newPassword, err := userToConvert.ConvertToSystem(utils.GetCtxUsername(ctx), convertJSON.CanWriteNotifications)
	if err != nil {
		AbortWithReturnError(ctx, http.StatusBadRequest, fmt.Errorf("Convert %s to system user failed", convertJSON.Username))
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"message":  "Verification successfull",
		"username": userToConvert.Username,
		"password": newPassword,
		"url":      fmt.Sprintf("%s://%s:%s%s", viper.GetString("exposed_scheme"), viper.GetString("exposed_host"), viper.GetString("exposed_port"), viper.GetString("exposed_path")),
	})
}
开发者ID:vmalguy,项目名称:tat,代码行数:35,代码来源:users.go

示例5: Delete

// Delete deletes requested topic only if user is Tat admin, or admin on topic
func (t *TopicsController) Delete(ctx *gin.Context) {

	topicRequest, err := GetParam(ctx, "topic")
	if err != nil {
		return
	}

	var user = models.User{}
	err = user.FindByUsername(utils.GetCtxUsername(ctx))
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."})
		return
	}

	paramJSON := paramTopicUserJSON{
		Topic:     topicRequest,
		Username:  user.Username,
		Recursive: false,
	}

	topic, e := t.preCheckUser(ctx, &paramJSON)
	if e != nil {
		return
	}
	err = topic.Delete(&user)
	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, errors.New(err.Error()))
		return
	}
	ctx.JSON(http.StatusOK, "")
}
开发者ID:vmalguy,项目名称:tat,代码行数:32,代码来源:topics.go

示例6: OneTopic

// OneTopic returns only requested topic, and only if user has read access
func (t *TopicsController) OneTopic(ctx *gin.Context) {
	topicRequest, err := GetParam(ctx, "topic")
	if err != nil {
		return
	}
	var user = models.User{}
	err = user.FindByUsername(utils.GetCtxUsername(ctx))
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."})
		return
	}
	topic := &models.Topic{}
	errfinding := topic.FindByTopic(topicRequest, user.IsAdmin)
	if errfinding != nil {
		ctx.JSON(http.StatusInternalServerError, errfinding)
		return
	}

	isReadAccess := topic.IsUserReadAccess(user)
	if !isReadAccess {
		ctx.JSON(http.StatusInternalServerError, errors.New("No Read Access to this topic: "+user.Username+" "+topic.Topic))
		return
	}
	out := &topicJSON{Topic: topic}
	ctx.JSON(http.StatusOK, out)
}
开发者ID:vmalguy,项目名称:tat,代码行数:27,代码来源:topics.go

示例7: listWithCriteria

func (m *PresencesController) listWithCriteria(ctx *gin.Context, criteria *models.PresenceCriteria) {
	user, e := m.preCheckUser(ctx)
	if e != nil {
		return
	}
	var topic = models.Topic{}
	err := topic.FindByTopic(criteria.Topic, true)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, errors.New("topic "+criteria.Topic+" does not exist"))
		return
	}

	isReadAccess := topic.IsUserReadAccess(user)
	if !isReadAccess {
		ctx.AbortWithError(http.StatusForbidden, errors.New("No Read Access to this topic."))
		return
	}
	// add / if search on topic
	// as topic is in path, it can't start with a /
	if criteria.Topic != "" && string(criteria.Topic[0]) != "/" {
		criteria.Topic = "/" + criteria.Topic
	}

	topicDM := "/Private/" + utils.GetCtxUsername(ctx) + "/DM/"
	if strings.HasPrefix(criteria.Topic, topicDM) {
		part := strings.Split(criteria.Topic, "/")
		if len(part) != 5 {
			log.Errorf("wrong topic name for DM")
			ctx.AbortWithError(http.StatusInternalServerError, errors.New("Wrong topic name for DM:"+criteria.Topic))
			return
		}
		topicInverse := "/Private/" + part[4] + "/DM/" + utils.GetCtxUsername(ctx)
		criteria.Topic = criteria.Topic + "," + topicInverse
	}

	count, presences, err := models.ListPresences(criteria)
	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return
	}
	out := &presencesJSON{
		Count:     count,
		Presences: presences,
	}
	ctx.JSON(http.StatusOK, out)
}
开发者ID:vmalguy,项目名称:tat,代码行数:46,代码来源:presences.go

示例8: preCheckUser

func (*PresencesController) preCheckUser(ctx *gin.Context) (models.User, error) {
	var user = models.User{}
	err := user.FindByUsername(utils.GetCtxUsername(ctx))
	if err != nil {
		e := errors.New("Error while fetching user.")
		ctx.AbortWithError(http.StatusInternalServerError, e)
		return user, e
	}
	return user, nil
}
开发者ID:vmalguy,项目名称:tat,代码行数:10,代码来源:presences.go

示例9: Me

// Me retrieves all information about me (exception information about Authentication)
func (*UsersController) Me(ctx *gin.Context) {
	var user = models.User{}
	err := user.FindByUsername(utils.GetCtxUsername(ctx))
	if err != nil {
		AbortWithReturnError(ctx, http.StatusInternalServerError, errors.New("Error while fetching user"))
		return
	}
	out := &userJSON{User: &user}
	ctx.JSON(http.StatusOK, out)
}
开发者ID:vmalguy,项目名称:tat,代码行数:11,代码来源:users.go

示例10: List

// List returns the list of topics that can be viewed by user
func (t *TopicsController) List(ctx *gin.Context) {
	criteria := t.buildCriteria(ctx)
	var user = &models.User{}
	err := user.FindByUsername(utils.GetCtxUsername(ctx))
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."})
		return
	}
	count, topics, err := models.ListTopics(criteria, user)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching topics."})
		return
	}

	out := &topicsJSON{Topics: topics, Count: count}

	if criteria.GetNbMsgUnread == "true" {
		c := &models.PresenceCriteria{
			Username: user.Username,
		}
		count, presences, err := models.ListPresencesAllFields(c)
		if err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		unread := make(map[string]int)
		knownPresence := false
		for _, topic := range topics {
			if utils.ArrayContains(user.OffNotificationsTopics, topic.Topic) {
				continue
			}
			knownPresence = false
			for _, presence := range presences {
				if topic.Topic != presence.Topic {
					continue
				}
				knownPresence = true

				nb, err := models.CountMsgSinceDate(presence.Topic, presence.DatePresence)
				if err != nil {
					ctx.JSON(http.StatusInternalServerError, err)
					return
				}
				unread[presence.Topic] = nb
			}
			if !knownPresence {
				unread[topic.Topic] = -1
			}
		}
		out.TopicsMsgUnread = unread
		out.CountTopicsMsgUnread = count
	}
	ctx.JSON(http.StatusOK, out)
}
开发者ID:vmalguy,项目名称:tat,代码行数:56,代码来源:topics.go

示例11: getTopicNonPrivateTasks

func (m *MessagesController) getTopicNonPrivateTasks(ctx *gin.Context, topics []string) (string, error) {
	// if msg is only in topic Tasks
	topicTasks := "/Private/" + utils.GetCtxUsername(ctx) + "/Tasks"
	for _, name := range topics {
		if !strings.HasPrefix(name, "/Private") {
			return name, nil
		}
		if !strings.HasPrefix(topics[0], topicTasks) {
			return name, nil
		}
	}
	return "", errors.New("Could not get non private task topic ")
}
开发者ID:vmalguy,项目名称:tat,代码行数:13,代码来源:messages.go

示例12: AddAdminUser

// AddAdminUser add a user to a group
func (g *GroupsController) AddAdminUser(ctx *gin.Context) {
	var paramJSON paramUserJSON
	ctx.Bind(&paramJSON)
	group, e := g.preCheckUser(ctx, &paramJSON)
	if e != nil {
		return
	}
	err := group.AddAdminUser(utils.GetCtxUsername(ctx), paramJSON.Username)
	if err != nil {
		return
	}
	ctx.JSON(http.StatusCreated, "")
}
开发者ID:vmalguy,项目名称:tat,代码行数:14,代码来源:groups.go

示例13: AddRoUser

// AddRoUser add a readonly user on selected topic
func (t *TopicsController) AddRoUser(ctx *gin.Context) {
	var paramJSON paramTopicUserJSON
	ctx.Bind(&paramJSON)
	topic, e := t.preCheckUser(ctx, &paramJSON)
	if e != nil {
		return
	}
	err := topic.AddRoUser(utils.GetCtxUsername(ctx), paramJSON.Username, paramJSON.Recursive)
	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, errors.New(err.Error()))
		return
	}
	ctx.JSON(http.StatusCreated, "")
}
开发者ID:vmalguy,项目名称:tat,代码行数:15,代码来源:topics.go

示例14: RemoveAdminGroup

// RemoveAdminGroup removes an admin group on selected topic
func (t *TopicsController) RemoveAdminGroup(ctx *gin.Context) {
	var paramJSON paramGroupJSON
	ctx.Bind(&paramJSON)
	topic, e := t.preCheckGroup(ctx, &paramJSON)
	if e != nil {
		return
	}

	err := topic.RemoveAdminGroup(utils.GetCtxUsername(ctx), paramJSON.Groupname, paramJSON.Recursive)
	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, errors.New(err.Error()))
		return
	}
	ctx.JSON(http.StatusOK, "")
}
开发者ID:vmalguy,项目名称:tat,代码行数:16,代码来源:topics.go

示例15: SetParam

// SetParam update Topic Parameters : MaxLength, CanForeceDate, CanUpdateMsg, CanDeleteMsg, CanUpdateAllMsg, CanDeleteAllMsg, IsROPublic
// admin only, except on Private topic
func (t *TopicsController) SetParam(ctx *gin.Context) {
	var paramJSON paramJSON
	ctx.Bind(&paramJSON)

	topic := models.Topic{}
	var err error
	if strings.HasPrefix(paramJSON.Topic, "/Private/"+utils.GetCtxUsername(ctx)) {
		err := topic.FindByTopic(paramJSON.Topic, false)
		if err != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching topic /Private/" + utils.GetCtxUsername(ctx)})
			return
		}
	} else {
		topic, err = t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic)
		if err != nil {
			ctx.JSON(http.StatusInternalServerError, err)
			return
		}
	}

	err = topic.SetParam(utils.GetCtxUsername(ctx),
		paramJSON.Recursive,
		paramJSON.MaxLength,
		paramJSON.CanForceDate,
		paramJSON.CanUpdateMsg,
		paramJSON.CanDeleteMsg,
		paramJSON.CanUpdateAllMsg,
		paramJSON.CanDeleteAllMsg,
		paramJSON.IsROPublic)

	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, errors.New(err.Error()))
		return
	}
	ctx.JSON(http.StatusCreated, "")
}
开发者ID:vmalguy,项目名称:tat,代码行数:38,代码来源:topics.go


注:本文中的github.com/ovh/tat/utils.GetCtxUsername函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。