示例#1
0
文件: topic.go 项目: ovh/tat
// Delete deletes a topic from database
func Delete(topic *tat.Topic, u *tat.User) error {
	log.Debugf("Delete: Clean topics cache for user %s", u.Username)
	if topic.Collection != "" {
		err := store.Tat().CTopics.Update(
			bson.M{"_id": topic.ID},
			bson.M{"$set": bson.M{"description": topic.Description + " TODEL"}})
		if err != nil {
			log.Errorf("Error while update description before delete topic %s err:%s", topic.Topic, err)
		}
		store.Tat().Session.SetMode(mgo.Strong, true)
		defer store.Tat().Session.SetMode(mgo.SecondaryPreferred, true)
		if err := store.Tat().Session.DB(store.DatabaseName).C(topic.Collection).DropCollection(); err != nil {
			return fmt.Errorf("Error while drop collection for topic %s err: %s", topic.Topic, err)
		}
	}

	if err := store.Tat().CTopics.Remove(bson.M{"_id": topic.ID}); err != nil {
		return fmt.Errorf("Error while remove topic from topics collection: %s", err)
	}
	cache.CleanAllTopicsLists()
	return nil
}
示例#2
0
文件: topic.go 项目: ovh/tat
// RemoveRwUser removes a read write user from topic
func RemoveRwUser(topic *tat.Topic, admin string, username string, recursive bool) error {
	err := actionOnSet(topic, "$pull", "rwUsers", username, admin, recursive, "remove from rw")
	cache.CleanAllTopicsLists()
	return err
}
示例#3
0
文件: topic.go 项目: ovh/tat
// AddAdminUser add a read write user to topic
func AddAdminUser(topic *tat.Topic, admin string, username string, recursive bool) error {
	err := actionOnSet(topic, "$addToSet", "adminUsers", username, admin, recursive, "add to admin")
	cache.CleanAllTopicsLists()
	return err
}
示例#4
0
文件: topic.go 项目: ovh/tat
// Insert creates a new topic. User is read write on topic
func Insert(topic *tat.Topic, u *tat.User) error {
	if err := CheckAndFixName(topic); err != nil {
		return err
	}

	isParentRootTopic, parentTopic, err := getParentTopic(topic)
	if !isParentRootTopic {
		if err != nil {
			return tat.NewError(http.StatusNotFound, "Parent Topic not found %s", topic.Topic)
		}
		// If user create a Topic in /Private/username, no check or RW to create
		if !strings.HasPrefix(topic.Topic, "/Private/"+u.Username) {
			// check if user can create topic in /topic
			hasRW := IsUserAdmin(parentTopic, u)
			if !hasRW {
				return tat.NewError(http.StatusUnauthorized, "No RW access to parent topic %s", parentTopic.Topic)
			}
		}
	} else if !u.IsAdmin { // no parent topic, check admin
		return tat.NewError(http.StatusUnauthorized, "No write access to create parent topic %s", topic.Topic)
	}
	if _, err = FindByTopic(topic.Topic, true, false, false, nil); err == nil {
		return tat.NewError(http.StatusConflict, "Topic Already Exists : %s", topic.Topic)
	}

	topic.ID = bson.NewObjectId().Hex()
	topic.DateCreation = time.Now().Unix()
	topic.MaxLength = tat.DefaultMessageMaxSize // topic MaxLenth messages
	topic.CanForceDate = false
	topic.IsAutoComputeLabels = true
	topic.IsAutoComputeTags = true
	topic.Collection = "messages" + topic.ID

	if !isParentRootTopic {
		topic.ROGroups = parentTopic.ROGroups
		topic.RWGroups = parentTopic.RWGroups
		topic.ROUsers = parentTopic.ROUsers
		topic.RWUsers = parentTopic.RWUsers
		topic.AdminUsers = parentTopic.AdminUsers
		topic.AdminGroups = parentTopic.AdminGroups
		topic.MaxLength = parentTopic.MaxLength
		topic.CanForceDate = parentTopic.CanForceDate
		// topic.CanUpdateMsg can be set by user.createTopics for new users
		// with CanUpdateMsg=true
		if !topic.CanUpdateMsg {
			topic.CanUpdateMsg = parentTopic.CanUpdateMsg
		}
		// topic.CanDeleteMsg can be set by user.createTopics for new users
		// with CanDeleteMsg=true
		if !topic.CanDeleteMsg {
			topic.CanDeleteMsg = parentTopic.CanDeleteMsg
		}
		topic.CanUpdateAllMsg = parentTopic.CanUpdateAllMsg
		topic.CanDeleteAllMsg = parentTopic.CanDeleteAllMsg
		topic.AdminCanUpdateAllMsg = parentTopic.AdminCanUpdateAllMsg
		topic.AdminCanDeleteAllMsg = parentTopic.AdminCanDeleteAllMsg
		topic.IsAutoComputeTags = parentTopic.IsAutoComputeTags
		topic.IsAutoComputeLabels = parentTopic.IsAutoComputeLabels
		topic.Parameters = parentTopic.Parameters
	}

	if err = store.Tat().CTopics.Insert(topic); err != nil {
		log.Errorf("Error while inserting new topic %s", err)
	}

	if errC := store.Tat().Session.DB(store.DatabaseName).C(topic.Collection).Create(&mgo.CollectionInfo{ForceIdIndex: true}); errC != nil {
		log.Errorf("Error while create new collection %s", topic.Collection)
	}

	store.EnsureIndexesMessages(topic.Collection)

	h := fmt.Sprintf("create a new topic :%s", topic.Topic)
	err = addToHistory(topic, bson.M{"_id": topic.ID}, u.Username, h)
	if err != nil {
		log.Errorf("Error while inserting history for new topic %s", err)
	}

	log.Debugf("Insert: Clean topics cache for user %s", u.Username)
	cache.CleanAllTopicsLists()

	return AddRwUser(topic, u.Username, u.Username, false)
}