user.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "bytes"
  7. "container/list"
  8. "crypto/sha256"
  9. "encoding/hex"
  10. "errors"
  11. "fmt"
  12. "image"
  13. "image/jpeg"
  14. "os"
  15. "path/filepath"
  16. "strings"
  17. "time"
  18. "github.com/Unknwon/com"
  19. "github.com/nfnt/resize"
  20. "github.com/gogits/gogs/modules/base"
  21. "github.com/gogits/gogs/modules/git"
  22. "github.com/gogits/gogs/modules/log"
  23. "github.com/gogits/gogs/modules/setting"
  24. )
  25. type UserType int
  26. const (
  27. INDIVIDUAL UserType = iota // Historic reason to make it starts at 0.
  28. ORGANIZATION
  29. )
  30. var (
  31. ErrUserOwnRepos = errors.New("User still have ownership of repositories")
  32. ErrUserHasOrgs = errors.New("User still have membership of organization")
  33. ErrUserAlreadyExist = errors.New("User already exist")
  34. ErrUserNotExist = errors.New("User does not exist")
  35. ErrUserNotKeyOwner = errors.New("User does not the owner of public key")
  36. ErrEmailAlreadyUsed = errors.New("E-mail already used")
  37. ErrUserNameIllegal = errors.New("User name contains illegal characters")
  38. ErrLoginSourceNotExist = errors.New("Login source does not exist")
  39. ErrLoginSourceNotActived = errors.New("Login source is not actived")
  40. ErrUnsupportedLoginType = errors.New("Login source is unknown")
  41. )
  42. // User represents the object of individual and member of organization.
  43. type User struct {
  44. Id int64
  45. LowerName string `xorm:"UNIQUE NOT NULL"`
  46. Name string `xorm:"UNIQUE NOT NULL"`
  47. FullName string
  48. Email string `xorm:"UNIQUE NOT NULL"`
  49. Passwd string `xorm:"NOT NULL"`
  50. LoginType LoginType
  51. LoginSource int64 `xorm:"NOT NULL DEFAULT 0"`
  52. LoginName string
  53. Type UserType
  54. Orgs []*User `xorm:"-"`
  55. Repos []*Repository `xorm:"-"`
  56. Location string
  57. Website string
  58. Rands string `xorm:"VARCHAR(10)"`
  59. Salt string `xorm:"VARCHAR(10)"`
  60. Created time.Time `xorm:"CREATED"`
  61. Updated time.Time `xorm:"UPDATED"`
  62. // Permissions.
  63. IsActive bool
  64. IsAdmin bool
  65. AllowGitHook bool
  66. // Avatar.
  67. Avatar string `xorm:"VARCHAR(2048) NOT NULL"`
  68. AvatarEmail string `xorm:"NOT NULL"`
  69. UseCustomAvatar bool
  70. // Counters.
  71. NumFollowers int
  72. NumFollowings int
  73. NumStars int
  74. NumRepos int
  75. // For organization.
  76. Description string
  77. NumTeams int
  78. NumMembers int
  79. Teams []*Team `xorm:"-"`
  80. Members []*User `xorm:"-"`
  81. }
  82. // DashboardLink returns the user dashboard page link.
  83. func (u *User) DashboardLink() string {
  84. if u.IsOrganization() {
  85. return setting.AppSubUrl + "/org/" + u.Name + "/dashboard/"
  86. }
  87. return setting.AppSubUrl + "/"
  88. }
  89. // HomeLink returns the user home page link.
  90. func (u *User) HomeLink() string {
  91. return setting.AppSubUrl + "/" + u.Name
  92. }
  93. // AvatarLink returns user gravatar link.
  94. func (u *User) AvatarLink() string {
  95. switch {
  96. case u.UseCustomAvatar:
  97. return setting.AppSubUrl + "/avatars/" + com.ToStr(u.Id)
  98. case setting.DisableGravatar:
  99. return setting.AppSubUrl + "/img/avatar_default.jpg"
  100. case setting.Service.EnableCacheAvatar:
  101. return setting.AppSubUrl + "/avatar/" + u.Avatar
  102. }
  103. return setting.GravatarSource + u.Avatar
  104. }
  105. // NewGitSig generates and returns the signature of given user.
  106. func (u *User) NewGitSig() *git.Signature {
  107. return &git.Signature{
  108. Name: u.Name,
  109. Email: u.Email,
  110. When: time.Now(),
  111. }
  112. }
  113. // EncodePasswd encodes password to safe format.
  114. func (u *User) EncodePasswd() {
  115. newPasswd := base.PBKDF2([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New)
  116. u.Passwd = fmt.Sprintf("%x", newPasswd)
  117. }
  118. // ValidtePassword checks if given password matches the one belongs to the user.
  119. func (u *User) ValidtePassword(passwd string) bool {
  120. newUser := &User{Passwd: passwd, Salt: u.Salt}
  121. newUser.EncodePasswd()
  122. return u.Passwd == newUser.Passwd
  123. }
  124. // UploadAvatar saves custom avatar for user.
  125. // FIXME: splite uploads to different subdirs in case we have massive users.
  126. func (u *User) UploadAvatar(data []byte) error {
  127. savePath := filepath.Join(setting.AvatarUploadPath, com.ToStr(u.Id))
  128. u.UseCustomAvatar = true
  129. img, _, err := image.Decode(bytes.NewReader(data))
  130. if err != nil {
  131. return err
  132. }
  133. m := resize.Resize(200, 200, img, resize.NearestNeighbor)
  134. sess := x.NewSession()
  135. defer sess.Close()
  136. if err = sess.Begin(); err != nil {
  137. return err
  138. }
  139. if _, err = sess.Id(u.Id).AllCols().Update(u); err != nil {
  140. sess.Rollback()
  141. return err
  142. }
  143. os.MkdirAll(setting.AvatarUploadPath, os.ModePerm)
  144. fw, err := os.Create(savePath)
  145. if err != nil {
  146. sess.Rollback()
  147. return err
  148. }
  149. defer fw.Close()
  150. if err = jpeg.Encode(fw, m, nil); err != nil {
  151. sess.Rollback()
  152. return err
  153. }
  154. return sess.Commit()
  155. }
  156. // IsOrganization returns true if user is actually a organization.
  157. func (u *User) IsOrganization() bool {
  158. return u.Type == ORGANIZATION
  159. }
  160. // IsUserOrgOwner returns true if user is in the owner team of given organization.
  161. func (u *User) IsUserOrgOwner(orgId int64) bool {
  162. return IsOrganizationOwner(orgId, u.Id)
  163. }
  164. // IsPublicMember returns true if user public his/her membership in give organization.
  165. func (u *User) IsPublicMember(orgId int64) bool {
  166. return IsPublicMembership(orgId, u.Id)
  167. }
  168. // GetOrganizationCount returns count of membership of organization of user.
  169. func (u *User) GetOrganizationCount() (int64, error) {
  170. return x.Where("uid=?", u.Id).Count(new(OrgUser))
  171. }
  172. // GetRepositories returns all repositories that user owns, including private repositories.
  173. func (u *User) GetRepositories() (err error) {
  174. u.Repos, err = GetRepositories(u.Id, true)
  175. return err
  176. }
  177. // GetOrganizations returns all organizations that user belongs to.
  178. func (u *User) GetOrganizations() error {
  179. ous, err := GetOrgUsersByUserId(u.Id)
  180. if err != nil {
  181. return err
  182. }
  183. u.Orgs = make([]*User, len(ous))
  184. for i, ou := range ous {
  185. u.Orgs[i], err = GetUserById(ou.OrgId)
  186. if err != nil {
  187. return err
  188. }
  189. }
  190. return nil
  191. }
  192. // GetFullNameFallback returns Full Name if set, otherwise username
  193. func (u *User) GetFullNameFallback() string {
  194. if u.FullName == "" {
  195. return u.Name
  196. }
  197. return u.FullName
  198. }
  199. // IsUserExist checks if given user name exist,
  200. // the user name should be noncased unique.
  201. func IsUserExist(name string) (bool, error) {
  202. if len(name) == 0 {
  203. return false, nil
  204. }
  205. return x.Get(&User{LowerName: strings.ToLower(name)})
  206. }
  207. // IsEmailUsed returns true if the e-mail has been used.
  208. func IsEmailUsed(email string) (bool, error) {
  209. if len(email) == 0 {
  210. return false, nil
  211. }
  212. return x.Get(&User{Email: email})
  213. }
  214. // GetUserSalt returns a ramdom user salt token.
  215. func GetUserSalt() string {
  216. return base.GetRandomString(10)
  217. }
  218. // CreateUser creates record of a new user.
  219. func CreateUser(u *User) error {
  220. if !IsLegalName(u.Name) {
  221. return ErrUserNameIllegal
  222. }
  223. isExist, err := IsUserExist(u.Name)
  224. if err != nil {
  225. return err
  226. } else if isExist {
  227. return ErrUserAlreadyExist
  228. }
  229. isExist, err = IsEmailUsed(u.Email)
  230. if err != nil {
  231. return err
  232. } else if isExist {
  233. return ErrEmailAlreadyUsed
  234. }
  235. u.LowerName = strings.ToLower(u.Name)
  236. u.Avatar = base.EncodeMd5(u.Email)
  237. u.AvatarEmail = u.Email
  238. u.Rands = GetUserSalt()
  239. u.Salt = GetUserSalt()
  240. u.EncodePasswd()
  241. sess := x.NewSession()
  242. defer sess.Close()
  243. if err = sess.Begin(); err != nil {
  244. return err
  245. }
  246. if _, err = sess.Insert(u); err != nil {
  247. sess.Rollback()
  248. return err
  249. } else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {
  250. sess.Rollback()
  251. return err
  252. } else if err = sess.Commit(); err != nil {
  253. return err
  254. }
  255. // Auto-set admin for user whose ID is 1.
  256. if u.Id == 1 {
  257. u.IsAdmin = true
  258. u.IsActive = true
  259. _, err = x.Id(u.Id).UseBool().Update(u)
  260. }
  261. return err
  262. }
  263. // CountUsers returns number of users.
  264. func CountUsers() int64 {
  265. count, _ := x.Where("type=0").Count(new(User))
  266. return count
  267. }
  268. // GetUsers returns given number of user objects with offset.
  269. func GetUsers(num, offset int) ([]*User, error) {
  270. users := make([]*User, 0, num)
  271. err := x.Limit(num, offset).Where("type=0").Asc("id").Find(&users)
  272. return users, err
  273. }
  274. // get user by erify code
  275. func getVerifyUser(code string) (user *User) {
  276. if len(code) <= base.TimeLimitCodeLength {
  277. return nil
  278. }
  279. // use tail hex username query user
  280. hexStr := code[base.TimeLimitCodeLength:]
  281. if b, err := hex.DecodeString(hexStr); err == nil {
  282. if user, err = GetUserByName(string(b)); user != nil {
  283. return user
  284. }
  285. log.Error(4, "user.getVerifyUser: %v", err)
  286. }
  287. return nil
  288. }
  289. // verify active code when active account
  290. func VerifyUserActiveCode(code string) (user *User) {
  291. minutes := setting.Service.ActiveCodeLives
  292. if user = getVerifyUser(code); user != nil {
  293. // time limit code
  294. prefix := code[:base.TimeLimitCodeLength]
  295. data := com.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
  296. if base.VerifyTimeLimitCode(data, minutes, prefix) {
  297. return user
  298. }
  299. }
  300. return nil
  301. }
  302. // ChangeUserName changes all corresponding setting from old user name to new one.
  303. func ChangeUserName(u *User, newUserName string) (err error) {
  304. if !IsLegalName(newUserName) {
  305. return ErrUserNameIllegal
  306. }
  307. newUserName = strings.ToLower(newUserName)
  308. // Update accesses of user.
  309. accesses := make([]Access, 0, 10)
  310. if err = x.Find(&accesses, &Access{UserName: u.LowerName}); err != nil {
  311. return err
  312. }
  313. sess := x.NewSession()
  314. defer sess.Close()
  315. if err = sess.Begin(); err != nil {
  316. return err
  317. }
  318. for i := range accesses {
  319. accesses[i].UserName = newUserName
  320. if strings.HasPrefix(accesses[i].RepoName, u.LowerName+"/") {
  321. accesses[i].RepoName = strings.Replace(accesses[i].RepoName, u.LowerName, newUserName, 1)
  322. }
  323. if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
  324. return err
  325. }
  326. }
  327. repos, err := GetRepositories(u.Id, true)
  328. if err != nil {
  329. return err
  330. }
  331. for i := range repos {
  332. accesses = make([]Access, 0, 10)
  333. // Update accesses of user repository.
  334. if err = x.Find(&accesses, &Access{RepoName: u.LowerName + "/" + repos[i].LowerName}); err != nil {
  335. return err
  336. }
  337. for j := range accesses {
  338. // if the access is not the user's access (already updated above)
  339. if accesses[j].UserName != u.LowerName {
  340. accesses[j].RepoName = newUserName + "/" + repos[i].LowerName
  341. if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil {
  342. return err
  343. }
  344. }
  345. }
  346. }
  347. // Change user directory name.
  348. if err = os.Rename(UserPath(u.LowerName), UserPath(newUserName)); err != nil {
  349. sess.Rollback()
  350. return err
  351. }
  352. return sess.Commit()
  353. }
  354. // UpdateUser updates user's information.
  355. func UpdateUser(u *User) error {
  356. u.LowerName = strings.ToLower(u.Name)
  357. if len(u.Location) > 255 {
  358. u.Location = u.Location[:255]
  359. }
  360. if len(u.Website) > 255 {
  361. u.Website = u.Website[:255]
  362. }
  363. if len(u.Description) > 255 {
  364. u.Description = u.Description[:255]
  365. }
  366. _, err := x.Id(u.Id).AllCols().Update(u)
  367. return err
  368. }
  369. // FIXME: need some kind of mechanism to record failure. HINT: system notice
  370. // DeleteUser completely and permanently deletes everything of user.
  371. func DeleteUser(u *User) error {
  372. // Check ownership of repository.
  373. count, err := GetRepositoryCount(u)
  374. if err != nil {
  375. return errors.New("GetRepositoryCount: " + err.Error())
  376. } else if count > 0 {
  377. return ErrUserOwnRepos
  378. }
  379. // Check membership of organization.
  380. count, err = u.GetOrganizationCount()
  381. if err != nil {
  382. return errors.New("GetOrganizationCount: " + err.Error())
  383. } else if count > 0 {
  384. return ErrUserHasOrgs
  385. }
  386. // FIXME: check issues, other repos' commits
  387. // FIXME: roll backable in some point.
  388. // Delete all followers.
  389. if _, err = x.Delete(&Follow{FollowId: u.Id}); err != nil {
  390. return err
  391. }
  392. // Delete oauth2.
  393. if _, err = x.Delete(&Oauth2{Uid: u.Id}); err != nil {
  394. return err
  395. }
  396. // Delete all feeds.
  397. if _, err = x.Delete(&Action{UserId: u.Id}); err != nil {
  398. return err
  399. }
  400. // Delete all watches.
  401. if _, err = x.Delete(&Watch{UserId: u.Id}); err != nil {
  402. return err
  403. }
  404. // Delete all accesses.
  405. if _, err = x.Delete(&Access{UserName: u.LowerName}); err != nil {
  406. return err
  407. }
  408. // Delete all SSH keys.
  409. keys := make([]*PublicKey, 0, 10)
  410. if err = x.Find(&keys, &PublicKey{OwnerId: u.Id}); err != nil {
  411. return err
  412. }
  413. for _, key := range keys {
  414. if err = DeletePublicKey(key); err != nil {
  415. return err
  416. }
  417. }
  418. // Delete user directory.
  419. if err = os.RemoveAll(UserPath(u.Name)); err != nil {
  420. return err
  421. }
  422. _, err = x.Delete(u)
  423. return err
  424. }
  425. // DeleteInactivateUsers deletes all inactivate users.
  426. func DeleteInactivateUsers() error {
  427. _, err := x.Where("is_active=?", false).Delete(new(User))
  428. return err
  429. }
  430. // UserPath returns the path absolute path of user repositories.
  431. func UserPath(userName string) string {
  432. return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
  433. }
  434. func GetUserByKeyId(keyId int64) (*User, error) {
  435. user := new(User)
  436. rawSql := "SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?"
  437. has, err := x.Sql(rawSql, keyId).Get(user)
  438. if err != nil {
  439. return nil, err
  440. } else if !has {
  441. return nil, ErrUserNotKeyOwner
  442. }
  443. return user, nil
  444. }
  445. // GetUserById returns the user object by given ID if exists.
  446. func GetUserById(id int64) (*User, error) {
  447. u := new(User)
  448. has, err := x.Id(id).Get(u)
  449. if err != nil {
  450. return nil, err
  451. } else if !has {
  452. return nil, ErrUserNotExist
  453. }
  454. return u, nil
  455. }
  456. // GetUserByName returns user by given name.
  457. func GetUserByName(name string) (*User, error) {
  458. if len(name) == 0 {
  459. return nil, ErrUserNotExist
  460. }
  461. u := &User{LowerName: strings.ToLower(name)}
  462. has, err := x.Get(u)
  463. if err != nil {
  464. return nil, err
  465. } else if !has {
  466. return nil, ErrUserNotExist
  467. }
  468. return u, nil
  469. }
  470. // GetUserEmailsByNames returns a list of e-mails corresponds to names.
  471. func GetUserEmailsByNames(names []string) []string {
  472. mails := make([]string, 0, len(names))
  473. for _, name := range names {
  474. u, err := GetUserByName(name)
  475. if err != nil {
  476. continue
  477. }
  478. mails = append(mails, u.Email)
  479. }
  480. return mails
  481. }
  482. // GetUserIdsByNames returns a slice of ids corresponds to names.
  483. func GetUserIdsByNames(names []string) []int64 {
  484. ids := make([]int64, 0, len(names))
  485. for _, name := range names {
  486. u, err := GetUserByName(name)
  487. if err != nil {
  488. continue
  489. }
  490. ids = append(ids, u.Id)
  491. }
  492. return ids
  493. }
  494. // UserCommit represtns a commit with validation of user.
  495. type UserCommit struct {
  496. User *User
  497. *git.Commit
  498. }
  499. // ValidateCommitWithEmail chceck if author's e-mail of commit is corresponsind to a user.
  500. func ValidateCommitWithEmail(c *git.Commit) *User {
  501. u, err := GetUserByEmail(c.Author.Email)
  502. if err != nil {
  503. return nil
  504. }
  505. return u
  506. }
  507. // ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
  508. func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
  509. emails := map[string]*User{}
  510. newCommits := list.New()
  511. e := oldCommits.Front()
  512. for e != nil {
  513. c := e.Value.(*git.Commit)
  514. var u *User
  515. if v, ok := emails[c.Author.Email]; !ok {
  516. u, _ = GetUserByEmail(c.Author.Email)
  517. emails[c.Author.Email] = u
  518. } else {
  519. u = v
  520. }
  521. newCommits.PushBack(UserCommit{
  522. User: u,
  523. Commit: c,
  524. })
  525. e = e.Next()
  526. }
  527. return newCommits
  528. }
  529. // GetUserByEmail returns the user object by given e-mail if exists.
  530. func GetUserByEmail(email string) (*User, error) {
  531. if len(email) == 0 {
  532. return nil, ErrUserNotExist
  533. }
  534. user := &User{Email: strings.ToLower(email)}
  535. has, err := x.Get(user)
  536. if err != nil {
  537. return nil, err
  538. } else if !has {
  539. return nil, ErrUserNotExist
  540. }
  541. return user, nil
  542. }
  543. // SearchUserByName returns given number of users whose name contains keyword.
  544. func SearchUserByName(opt SearchOption) (us []*User, err error) {
  545. opt.Keyword = FilterSQLInject(opt.Keyword)
  546. if len(opt.Keyword) == 0 {
  547. return us, nil
  548. }
  549. opt.Keyword = strings.ToLower(opt.Keyword)
  550. us = make([]*User, 0, opt.Limit)
  551. err = x.Limit(opt.Limit).Where("type=0").And("lower_name like ?", "%"+opt.Keyword+"%").Find(&us)
  552. return us, err
  553. }
  554. // Follow is connection request for receiving user notifycation.
  555. type Follow struct {
  556. Id int64
  557. UserId int64 `xorm:"unique(follow)"`
  558. FollowId int64 `xorm:"unique(follow)"`
  559. }
  560. // FollowUser marks someone be another's follower.
  561. func FollowUser(userId int64, followId int64) (err error) {
  562. sess := x.NewSession()
  563. defer sess.Close()
  564. sess.Begin()
  565. if _, err = sess.Insert(&Follow{UserId: userId, FollowId: followId}); err != nil {
  566. sess.Rollback()
  567. return err
  568. }
  569. rawSql := "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?"
  570. if _, err = sess.Exec(rawSql, followId); err != nil {
  571. sess.Rollback()
  572. return err
  573. }
  574. rawSql = "UPDATE `user` SET num_followings = num_followings + 1 WHERE id = ?"
  575. if _, err = sess.Exec(rawSql, userId); err != nil {
  576. sess.Rollback()
  577. return err
  578. }
  579. return sess.Commit()
  580. }
  581. // UnFollowUser unmarks someone be another's follower.
  582. func UnFollowUser(userId int64, unFollowId int64) (err error) {
  583. session := x.NewSession()
  584. defer session.Close()
  585. session.Begin()
  586. if _, err = session.Delete(&Follow{UserId: userId, FollowId: unFollowId}); err != nil {
  587. session.Rollback()
  588. return err
  589. }
  590. rawSql := "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?"
  591. if _, err = session.Exec(rawSql, unFollowId); err != nil {
  592. session.Rollback()
  593. return err
  594. }
  595. rawSql = "UPDATE `user` SET num_followings = num_followings - 1 WHERE id = ?"
  596. if _, err = session.Exec(rawSql, userId); err != nil {
  597. session.Rollback()
  598. return err
  599. }
  600. return session.Commit()
  601. }
  602. func UpdateMentions(userNames []string, issueId int64) error {
  603. users := make([]*User, 0, len(userNames))
  604. if err := x.Where("name IN (?)", strings.Join(userNames, "\",\"")).OrderBy("name ASC").Find(&users); err != nil {
  605. return err
  606. }
  607. ids := make([]int64, 0, len(userNames))
  608. for _, user := range users {
  609. ids = append(ids, user.Id)
  610. if user.Type == INDIVIDUAL {
  611. continue
  612. }
  613. if user.NumMembers == 0 {
  614. continue
  615. }
  616. tempIds := make([]int64, 0, user.NumMembers)
  617. orgUsers, err := GetOrgUsersByOrgId(user.Id)
  618. if err != nil {
  619. return err
  620. }
  621. for _, orgUser := range orgUsers {
  622. tempIds = append(tempIds, orgUser.Id)
  623. }
  624. ids = append(ids, tempIds...)
  625. }
  626. if err := UpdateIssueUserPairsByMentions(ids, issueId); err != nil {
  627. return err
  628. }
  629. return nil
  630. }