本文整理汇总了Golang中labix/org/v2/mgo/bson.ObjectId.Hex方法的典型用法代码示例。如果您正苦于以下问题:Golang ObjectId.Hex方法的具体用法?Golang ObjectId.Hex怎么用?Golang ObjectId.Hex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo/bson.ObjectId
的用法示例。
在下文中一共展示了ObjectId.Hex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CanModifyComment
// Only called when doing update or delete.
// At inserting, user.OkayToDoAction is sufficient.
// This needs additional action: you can only update or delete a given comment only if it's yours (under a certain level).
func CanModifyComment(db *mgo.Database, inp map[string][]string, correction_level int, user_id bson.ObjectId, user_level int) error {
rule := map[string]interface{}{
"content_id": "must",
"comment_id": "must",
"type": "must",
}
dat, err := extract.New(rule).Extract(inp)
if err != nil {
return err
}
// Even if he has the required level, and he is below correction_level, he can't modify other people's comment, only his owns.
// So we query here the comment and check who is the owner of it.
if user_level < correction_level {
content_id_str := basic.StripId(dat["content_id"].(string))
comment_id_str := basic.StripId(dat["comment_id"].(string))
auth, err := findCommentAuthor(db, content_id_str, comment_id_str)
if err != nil {
return err
}
if auth.Hex() != user_id.Hex() {
return fmt.Errorf("You are not the rightous owner of the comment.")
}
}
return nil
}
示例2: GetRead
func (c Blog) GetRead(id bson.ObjectId) revel.Result {
if id.Hex() != "" {
article := models.GetArticleByObjectId(c.MongoSession, id)
return c.Render(article)
}
return c.NotFound("Invalid article Id.")
}
示例3: GetId
// Get document from id
func (s *session) GetId(col string, id bson.ObjectId, doc interface{}) error {
c := s.db.C(col)
err := c.FindId(id).One(doc)
if err != nil {
log.Printf("**ERROR: Could not lookup document with id %v from collection %v: %v", id.Hex(), col, err)
}
return err
}
示例4: AddSharedNote
// 添加共享d笔记
func (this *NoteService) AddSharedNote(note info.Note, myUserId bson.ObjectId) info.Note {
// 判断我是否有权限添加
if shareService.HasUpdateNotebookPerm(note.UserId.Hex(), myUserId.Hex(), note.NotebookId.Hex()) {
note.CreatedUserId = myUserId // 是我给共享我的人创建的
return this.AddNote(note)
}
return info.Note{}
}
示例5: ObjectIdInSlice
// 查找ObjectId数组中是否有指定值.
func ObjectIdInSlice(id bson.ObjectId, list []bson.ObjectId) bool {
for _, b := range list {
if b.Hex() == id.Hex() {
return true
}
}
return false
}
示例6: backupServer
func backupServer(serverId string, backupTime time.Time) (err error) {
wip := <-wipGen.C
defer close(wip)
server := servers.Get(serverId)
if server == nil {
err = errors.New(fmt.Sprintf("no server for %s", serverId))
return
}
var (
url string
size int64
)
err = retry(1000, 5*time.Second, func(retries int) error {
var err error
url, size, err = server.BackupWorld(backupTime)
if err != nil {
plog.Error(err, map[string]interface{}{
"event": "world_backup",
"serverId": serverId,
"retries": retries,
})
}
return err
})
if err != nil {
return
}
var snapshotId bson.ObjectId
err = retry(1000, 5*time.Second, func(retries int) error {
var err error
snapshotId, err = StoreBackupInMongo(serverId, url, size, backupTime)
if err != nil {
plog.Error(err, map[string]interface{}{
"event": "world_db_store",
"serverId": serverId,
"retries": retries,
})
}
return err
})
pushPinkyEvent(map[string]interface{}{
"ts": time.Now(),
"server_id": serverId,
"event": "server_event",
"type": "backed_up",
"snapshot_id": snapshotId.Hex(),
"url": url,
"size": size,
})
return
}
示例7: deleteImage
func (api *EventsApi) deleteImage(id bson.ObjectId) error {
err := api.images.DeleteFile(api.images.GetBasePath() + string(os.PathSeparator) + id.Hex() + ".jpg")
if err != nil {
return err
}
api.imageCache.Remove(api.imageCache.FindKeys(id.Hex()))
return nil
}
示例8: Authenticate
//-------------------------------------------------------
// methods
//-------------------------------------------------------
// obtain all the data and proceed to authorisation
func (d *Daemon) Authenticate(id bson.ObjectId) error {
c := db.C("daemons")
err := c.FindId(id).One(&d.Entry)
if err == nil {
d.Id = id.Hex()
d.OrgId = d.Entry.OrgId.Hex()
return d.Authorise()
}
return err
}
示例9: mapFromFeed
func mapFromFeed(feed *domain.Feed) feedDao {
var id bson.ObjectId
if len(feed.ID) > 0 {
id = bson.ObjectIdHex(feed.ID)
} else {
id = bson.NewObjectId()
feed.ID = id.Hex()
}
return feedDao{id, feed.Uri, feed.LastSync}
}
示例10: Authenticate
//-------------------------------------------------------
// methods
//-------------------------------------------------------
// obtain all the data and proceed to authorisation
func (u *User) Authenticate(id bson.ObjectId) error {
c := db.C("users")
err := c.FindId(id).One(&u.Entry)
if err == nil {
u.Id = id.Hex()
u.OrgId = u.Entry.OrgId.Hex()
return u.Authorise()
}
return err
}
示例11: ObjectIdIsIn
// 检测ObjectId数组中是否存在某一个id。
func ObjectIdIsIn(ids []bson.ObjectId, id bson.ObjectId) bool {
result := false
for i := 0; i < len(ids); i++ {
if ids[i].Hex() == id.Hex() {
result = true
break
}
}
return result
}
示例12: GetUpdate
func (c User) GetUpdate(id bson.ObjectId) revel.Result {
if c.ActiveUser != nil {
action := "/User/" + id.Hex()
user := c.ActiveUser
if user.CanBeUpdatedBy(c.MongoSession, c.ActiveUser) {
return c.Render(action, user)
}
return c.Forbidden("You are not allowed to edit this resource.")
}
return c.Redirect(User.GetLogin)
}
示例13: SetBSON
// Implements bson.Setter for mongo.Ref compatibility.
// Yes this bson dependency is dirty, but necessary
// for transition form mongo.Ref to model.Ref.
func (self *Ref) SetBSON(raw bson.Raw) error {
var objectId *bson.ObjectId
if raw.Unmarshal(&objectId) == nil {
if objectId != nil {
*self = Ref(objectId.Hex())
} else {
*self = ""
}
return nil
}
return raw.Unmarshal(self)
}
示例14: CheckForId
func (table *mongoTable) CheckForId(id bson.ObjectId) error {
count, err := table.CountById(id)
if err != nil {
return err
}
if count == 1 {
return nil
} else {
return errors.New(table.collection.Name + " " + id.Hex() + " not found.")
}
}
示例15: Login
// Sets a cookie to w named "user" with a value of the encoded user_id.
// Admins, guests, registered users, everyone logs in with this.
func Login(w http.ResponseWriter, user_id bson.ObjectId, block_key []byte) error {
id_b, err := encryptStr(block_key, user_id.Hex())
if err != nil {
return err
}
encoded_id := string(id_b)
c := &http.Cookie{
Name: "user",
Value: encoded_id,
MaxAge: 3600000,
Path: "/",
}
http.SetCookie(w, c)
return nil
}