user.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. fw, err := os.Create(savePath)
  144. if err != nil {
  145. sess.Rollback()
  146. return err
  147. }
  148. defer fw.Close()
  149. if err = jpeg.Encode(fw, m, nil); err != nil {
  150. sess.Rollback()
  151. return err
  152. }
  153. return sess.Commit()
  154. }
  155. // IsOrganization returns true if user is actually a organization.
  156. func (u *User) IsOrganization() bool {
  157. return u.Type == ORGANIZATION
  158. }
  159. // IsUserOrgOwner returns true if user is in the owner team of given organization.
  160. func (u *User) IsUserOrgOwner(orgId int64) bool {
  161. return IsOrganizationOwner(orgId, u.Id)
  162. }
  163. // IsPublicMember returns true if user public his/her membership in give organization.
  164. func (u *User) IsPublicMember(orgId int64) bool {
  165. return IsPublicMembership(orgId, u.Id)
  166. }
  167. // GetOrganizationCount returns count of membership of organization of user.
  168. func (u *User) GetOrganizationCount() (int64, error) {
  169. return x.Where("uid=?", u.Id).Count(new(OrgUser))
  170. }
  171. // GetRepositories returns all repositories that user owns, including private repositories.
  172. func (u *User) GetRepositories() (err error) {
  173. u.Repos, err = GetRepositories(u.Id, true)
  174. return err
  175. }
  176. // GetOrganizations returns all organizations that user belongs to.
  177. func (u *User) GetOrganizations() error {
  178. ous, err := GetOrgUsersByUserId(u.Id)
  179. if err != nil {
  180. return err
  181. }
  182. u.Orgs = make([]*User, len(ous))
  183. for i, ou := range ous {
  184. u.Orgs[i], err = GetUserById(ou.OrgId)
  185. if err != nil {
  186. return err
  187. }
  188. }
  189. return nil
  190. }
  191. // GetFullNameFallback returns Full Name if set, otherwise username
  192. func (u *User) GetFullNameFallback() string {
  193. if u.FullName == "" {
  194. return u.Name
  195. }
  196. return u.FullName
  197. }
  198. // IsUserExist checks if given user name exist,
  199. // the user name should be noncased unique.
  200. func IsUserExist(name string) (bool, error) {
  201. if len(name) == 0 {
  202. return false, nil
  203. }
  204. return x.Get(&User{LowerName: strings.ToLower(name)})
  205. }
  206. // IsEmailUsed returns true if the e-mail has been used.
  207. func IsEmailUsed(email string) (bool, error) {
  208. if len(email) == 0 {
  209. return false, nil
  210. }
  211. return x.Get(&User{Email: email})
  212. }
  213. // GetUserSalt returns a ramdom user salt token.
  214. func GetUserSalt() string {
  215. return base.GetRandomString(10)
  216. }
  217. // CreateUser creates record of a new user.
  218. func CreateUser(u *User) error {
  219. if !IsLegalName(u.Name) {
  220. return ErrUserNameIllegal
  221. }
  222. isExist, err := IsUserExist(u.Name)
  223. if err != nil {
  224. return err
  225. } else if isExist {
  226. return ErrUserAlreadyExist
  227. }
  228. isExist, err = IsEmailUsed(u.Email)
  229. if err != nil {
  230. return err
  231. } else if isExist {
  232. return ErrEmailAlreadyUsed
  233. }
  234. u.LowerName = strings.ToLower(u.Name)
  235. u.Avatar = base.EncodeMd5(u.Email)
  236. u.AvatarEmail = u.Email
  237. u.Rands = GetUserSalt()
  238. u.Salt = GetUserSalt()
  239. u.EncodePasswd()
  240. sess := x.NewSession()
  241. defer sess.Close()
  242. if err = sess.Begin(); err != nil {
  243. return err
  244. }
  245. if _, err = sess.Insert(u); err != nil {
  246. sess.Rollback()
  247. return err
  248. } else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {
  249. sess.Rollback()
  250. return err
  251. } else if err = sess.Commit(); err != nil {
  252. return err
  253. }
  254. // Auto-set admin for user whose ID is 1.
  255. if u.Id == 1 {
  256. u.IsAdmin = true
  257. u.IsActive = true
  258. _, err = x.Id(u.Id).UseBool().Update(u)
  259. }
  260. return err
  261. }
  262. // CountUsers returns number of users.
  263. func CountUsers() int64 {
  264. count, _ := x.Where("type=0").Count(new(User))
  265. return count
  266. }
  267. // GetUsers returns given number of user objects with offset.
  268. func GetUsers(num, offset int) ([]*User, error) {
  269. users := make([]*User, 0, num)
  270. err := x.Limit(num, offset).Where("type=0").Asc("id").Find(&users)
  271. return users, err
  272. }
  273. // get user by erify code
  274. func getVerifyUser(code string) (user *User) {
  275. if len(code) <= base.TimeLimitCodeLength {
  276. return nil
  277. }
  278. // use tail hex username query user
  279. hexStr := code[base.TimeLimitCodeLength:]
  280. if b, err := hex.DecodeString(hexStr); err == nil {
  281. if user, err = GetUserByName(string(b)); user != nil {
  282. return user
  283. }
  284. log.Error(4, "user.getVerifyUser: %v", err)
  285. }
  286. return nil
  287. }
  288. // verify active code when active account
  289. func VerifyUserActiveCode(code string) (user *User) {
  290. minutes := setting.Service.ActiveCodeLives
  291. if user = getVerifyUser(code); user != nil {
  292. // time limit code
  293. prefix := code[:base.TimeLimitCodeLength]
  294. data := com.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
  295. if base.VerifyTimeLimitCode(data, minutes, prefix) {
  296. return user
  297. }
  298. }
  299. return nil
  300. }
  301. // ChangeUserName changes all corresponding setting from old user name to new one.
  302. func ChangeUserName(u *User, newUserName string) (err error) {
  303. if !IsLegalName(newUserName) {
  304. return ErrUserNameIllegal
  305. }
  306. newUserName = strings.ToLower(newUserName)
  307. // Update accesses of user.
  308. accesses := make([]Access, 0, 10)
  309. if err = x.Find(&accesses, &Access{UserName: u.LowerName}); err != nil {
  310. return err
  311. }
  312. sess := x.NewSession()
  313. defer sess.Close()
  314. if err = sess.Begin(); err != nil {
  315. return err
  316. }
  317. for i := range accesses {
  318. accesses[i].UserName = newUserName
  319. if strings.HasPrefix(accesses[i].RepoName, u.LowerName+"/") {
  320. accesses[i].RepoName = strings.Replace(accesses[i].RepoName, u.LowerName, newUserName, 1)
  321. }
  322. if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
  323. return err
  324. }
  325. }
  326. repos, err := GetRepositories(u.Id, true)
  327. if err != nil {
  328. return err
  329. }
  330. for i := range repos {
  331. accesses = make([]Access, 0, 10)
  332. // Update accesses of user repository.
  333. if err = x.Find(&accesses, &Access{RepoName: u.LowerName + "/" + repos[i].LowerName}); err != nil {
  334. return err
  335. }
  336. for j := range accesses {
  337. // if the access is not the user's access (already updated above)
  338. if accesses[j].UserName != u.LowerName {
  339. accesses[j].RepoName = newUserName + "/" + repos[i].LowerName
  340. if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil {
  341. return err
  342. }
  343. }
  344. }
  345. }
  346. // Change user directory name.
  347. if err = os.Rename(UserPath(u.LowerName), UserPath(newUserName)); err != nil {
  348. sess.Rollback()
  349. return err
  350. }
  351. return sess.Commit()
  352. }
  353. // UpdateUser updates user's information.
  354. func UpdateUser(u *User) error {
  355. u.LowerName = strings.ToLower(u.Name)
  356. if len(u.Location) > 255 {
  357. u.Location = u.Location[:255]
  358. }
  359. if len(u.Website) > 255 {
  360. u.Website = u.Website[:255]
  361. }
  362. if len(u.Description) > 255 {
  363. u.Description = u.Description[:255]
  364. }
  365. _, err := x.Id(u.Id).AllCols().Update(u)
  366. return err
  367. }
  368. // FIXME: need some kind of mechanism to record failure. HINT: system notice
  369. // DeleteUser completely and permanently deletes everything of user.
  370. func DeleteUser(u *User) error {
  371. // Check ownership of repository.
  372. count, err := GetRepositoryCount(u)
  373. if err != nil {
  374. return errors.New("GetRepositoryCount: " + err.Error())
  375. } else if count > 0 {
  376. return ErrUserOwnRepos
  377. }
  378. // Check membership of organization.
  379. count, err = u.GetOrganizationCount()
  380. if err != nil {
  381. return errors.New("GetOrganizationCount: " + err.Error())
  382. } else if count > 0 {
  383. return ErrUserHasOrgs
  384. }
  385. // FIXME: check issues, other repos' commits
  386. // FIXME: roll backable in some point.
  387. // Delete all followers.
  388. if _, err = x.Delete(&Follow{FollowId: u.Id}); err != nil {
  389. return err
  390. }
  391. // Delete oauth2.
  392. if _, err = x.Delete(&Oauth2{Uid: u.Id}); err != nil {
  393. return err
  394. }
  395. // Delete all feeds.
  396. if _, err = x.Delete(&Action{UserId: u.Id}); err != nil {
  397. return err
  398. }
  399. // Delete all watches.
  400. if _, err = x.Delete(&Watch{UserId: u.Id}); err != nil {
  401. return err
  402. }
  403. // Delete all accesses.
  404. if _, err = x.Delete(&Access{UserName: u.LowerName}); err != nil {
  405. return err
  406. }
  407. // Delete all SSH keys.
  408. keys := make([]*PublicKey, 0, 10)
  409. if err = x.Find(&keys, &PublicKey{OwnerId: u.Id}); err != nil {
  410. return err
  411. }
  412. for _, key := range keys {
  413. if err = DeletePublicKey(key); err != nil {
  414. return err
  415. }
  416. }
  417. // Delete user directory.
  418. if err = os.RemoveAll(UserPath(u.Name)); err != nil {
  419. return err
  420. }
  421. _, err = x.Delete(u)
  422. return err
  423. }
  424. // DeleteInactivateUsers deletes all inactivate users.
  425. func DeleteInactivateUsers() error {
  426. _, err := x.Where("is_active=?", false).Delete(new(User))
  427. return err
  428. }
  429. // UserPath returns the path absolute path of user repositories.
  430. func UserPath(userName string) string {
  431. return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
  432. }
  433. func GetUserByKeyId(keyId int64) (*User, error) {
  434. user := new(User)
  435. rawSql := "SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?"
  436. has, err := x.Sql(rawSql, keyId).Get(user)
  437. if err != nil {
  438. return nil, err
  439. } else if !has {
  440. return nil, ErrUserNotKeyOwner
  441. }
  442. return user, nil
  443. }
  444. // GetUserById returns the user object by given ID if exists.
  445. func GetUserById(id int64) (*User, error) {
  446. u := new(User)
  447. has, err := x.Id(id).Get(u)
  448. if err != nil {
  449. return nil, err
  450. } else if !has {
  451. return nil, ErrUserNotExist
  452. }
  453. return u, nil
  454. }
  455. // GetUserByName returns user by given name.
  456. func GetUserByName(name string) (*User, error) {
  457. if len(name) == 0 {
  458. return nil, ErrUserNotExist
  459. }
  460. u := &User{LowerName: strings.ToLower(name)}
  461. has, err := x.Get(u)
  462. if err != nil {
  463. return nil, err
  464. } else if !has {
  465. return nil, ErrUserNotExist
  466. }
  467. return u, nil
  468. }
  469. // GetUserEmailsByNames returns a list of e-mails corresponds to names.
  470. func GetUserEmailsByNames(names []string) []string {
  471. mails := make([]string, 0, len(names))
  472. for _, name := range names {
  473. u, err := GetUserByName(name)
  474. if err != nil {
  475. continue
  476. }
  477. mails = append(mails, u.Email)
  478. }
  479. return mails
  480. }
  481. // GetUserIdsByNames returns a slice of ids corresponds to names.
  482. func GetUserIdsByNames(names []string) []int64 {
  483. ids := make([]int64, 0, len(names))
  484. for _, name := range names {
  485. u, err := GetUserByName(name)
  486. if err != nil {
  487. continue
  488. }
  489. ids = append(ids, u.Id)
  490. }
  491. return ids
  492. }
  493. // UserCommit represtns a commit with validation of user.
  494. type UserCommit struct {
  495. User *User
  496. *git.Commit
  497. }
  498. // ValidateCommitWithEmail chceck if author's e-mail of commit is corresponsind to a user.
  499. func ValidateCommitWithEmail(c *git.Commit) *User {
  500. u, err := GetUserByEmail(c.Author.Email)
  501. if err != nil {
  502. return nil
  503. }
  504. return u
  505. }
  506. // ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
  507. func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
  508. emails := map[string]*User{}
  509. newCommits := list.New()
  510. e := oldCommits.Front()
  511. for e != nil {
  512. c := e.Value.(*git.Commit)
  513. var u *User
  514. if v, ok := emails[c.Author.Email]; !ok {
  515. u, _ = GetUserByEmail(c.Author.Email)
  516. emails[c.Author.Email] = u
  517. } else {
  518. u = v
  519. }
  520. newCommits.PushBack(UserCommit{
  521. User: u,
  522. Commit: c,
  523. })
  524. e = e.Next()
  525. }
  526. return newCommits
  527. }
  528. // GetUserByEmail returns the user object by given e-mail if exists.
  529. func GetUserByEmail(email string) (*User, error) {
  530. if len(email) == 0 {
  531. return nil, ErrUserNotExist
  532. }
  533. user := &User{Email: strings.ToLower(email)}
  534. has, err := x.Get(user)
  535. if err != nil {
  536. return nil, err
  537. } else if !has {
  538. return nil, ErrUserNotExist
  539. }
  540. return user, nil
  541. }
  542. // SearchUserByName returns given number of users whose name contains keyword.
  543. func SearchUserByName(opt SearchOption) (us []*User, err error) {
  544. opt.Keyword = FilterSQLInject(opt.Keyword)
  545. if len(opt.Keyword) == 0 {
  546. return us, nil
  547. }
  548. opt.Keyword = strings.ToLower(opt.Keyword)
  549. us = make([]*User, 0, opt.Limit)
  550. err = x.Limit(opt.Limit).Where("type=0").And("lower_name like ?", "%"+opt.Keyword+"%").Find(&us)
  551. return us, err
  552. }
  553. // Follow is connection request for receiving user notifycation.
  554. type Follow struct {
  555. Id int64
  556. UserId int64 `xorm:"unique(follow)"`
  557. FollowId int64 `xorm:"unique(follow)"`
  558. }
  559. // FollowUser marks someone be another's follower.
  560. func FollowUser(userId int64, followId int64) (err error) {
  561. sess := x.NewSession()
  562. defer sess.Close()
  563. sess.Begin()
  564. if _, err = sess.Insert(&Follow{UserId: userId, FollowId: followId}); err != nil {
  565. sess.Rollback()
  566. return err
  567. }
  568. rawSql := "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?"
  569. if _, err = sess.Exec(rawSql, followId); err != nil {
  570. sess.Rollback()
  571. return err
  572. }
  573. rawSql = "UPDATE `user` SET num_followings = num_followings + 1 WHERE id = ?"
  574. if _, err = sess.Exec(rawSql, userId); err != nil {
  575. sess.Rollback()
  576. return err
  577. }
  578. return sess.Commit()
  579. }
  580. // UnFollowUser unmarks someone be another's follower.
  581. func UnFollowUser(userId int64, unFollowId int64) (err error) {
  582. session := x.NewSession()
  583. defer session.Close()
  584. session.Begin()
  585. if _, err = session.Delete(&Follow{UserId: userId, FollowId: unFollowId}); err != nil {
  586. session.Rollback()
  587. return err
  588. }
  589. rawSql := "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?"
  590. if _, err = session.Exec(rawSql, unFollowId); err != nil {
  591. session.Rollback()
  592. return err
  593. }
  594. rawSql = "UPDATE `user` SET num_followings = num_followings - 1 WHERE id = ?"
  595. if _, err = session.Exec(rawSql, userId); err != nil {
  596. session.Rollback()
  597. return err
  598. }
  599. return session.Commit()
  600. }
  601. func UpdateMentions(userNames []string, issueId int64) error {
  602. users := make([]*User, 0, len(userNames))
  603. if err := x.Where("name IN (?)", strings.Join(userNames, "\",\"")).OrderBy("name ASC").Find(&users); err != nil {
  604. return err
  605. }
  606. ids := make([]int64, 0, len(userNames))
  607. for _, user := range users {
  608. ids = append(ids, user.Id)
  609. if user.Type == INDIVIDUAL {
  610. continue
  611. }
  612. if user.NumMembers == 0 {
  613. continue
  614. }
  615. tempIds := make([]int64, 0, user.NumMembers)
  616. orgUsers, err := GetOrgUsersByOrgId(user.Id)
  617. if err != nil {
  618. return err
  619. }
  620. for _, orgUser := range orgUsers {
  621. tempIds = append(tempIds, orgUser.Id)
  622. }
  623. ids = append(ids, tempIds...)
  624. }
  625. if err := UpdateIssueUserPairsByMentions(ids, issueId); err != nil {
  626. return err
  627. }
  628. return nil
  629. }