You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 17KB

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