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.

600 lines
14 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cmd
  6. import (
  7. "errors"
  8. "fmt"
  9. "os"
  10. "text/tabwriter"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth/oauth2"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/graceful"
  15. "code.gitea.io/gitea/modules/log"
  16. pwd "code.gitea.io/gitea/modules/password"
  17. repo_module "code.gitea.io/gitea/modules/repository"
  18. "code.gitea.io/gitea/modules/setting"
  19. "github.com/urfave/cli"
  20. )
  21. var (
  22. // CmdAdmin represents the available admin sub-command.
  23. CmdAdmin = cli.Command{
  24. Name: "admin",
  25. Usage: "Command line interface to perform common administrative operations",
  26. Subcommands: []cli.Command{
  27. subcmdCreateUser,
  28. subcmdChangePassword,
  29. subcmdRepoSyncReleases,
  30. subcmdRegenerate,
  31. subcmdAuth,
  32. },
  33. }
  34. subcmdCreateUser = cli.Command{
  35. Name: "create-user",
  36. Usage: "Create a new user in database",
  37. Action: runCreateUser,
  38. Flags: []cli.Flag{
  39. cli.StringFlag{
  40. Name: "name",
  41. Usage: "Username. DEPRECATED: use username instead",
  42. },
  43. cli.StringFlag{
  44. Name: "username",
  45. Usage: "Username",
  46. },
  47. cli.StringFlag{
  48. Name: "password",
  49. Usage: "User password",
  50. },
  51. cli.StringFlag{
  52. Name: "email",
  53. Usage: "User email address",
  54. },
  55. cli.BoolFlag{
  56. Name: "admin",
  57. Usage: "User is an admin",
  58. },
  59. cli.BoolFlag{
  60. Name: "random-password",
  61. Usage: "Generate a random password for the user",
  62. },
  63. cli.BoolFlag{
  64. Name: "must-change-password",
  65. Usage: "Set this option to false to prevent forcing the user to change their password after initial login, (Default: true)",
  66. },
  67. cli.IntFlag{
  68. Name: "random-password-length",
  69. Usage: "Length of the random password to be generated",
  70. Value: 12,
  71. },
  72. cli.BoolFlag{
  73. Name: "access-token",
  74. Usage: "Generate access token for the user",
  75. },
  76. },
  77. }
  78. subcmdChangePassword = cli.Command{
  79. Name: "change-password",
  80. Usage: "Change a user's password",
  81. Action: runChangePassword,
  82. Flags: []cli.Flag{
  83. cli.StringFlag{
  84. Name: "username,u",
  85. Value: "",
  86. Usage: "The user to change password for",
  87. },
  88. cli.StringFlag{
  89. Name: "password,p",
  90. Value: "",
  91. Usage: "New password to set for user",
  92. },
  93. },
  94. }
  95. subcmdRepoSyncReleases = cli.Command{
  96. Name: "repo-sync-releases",
  97. Usage: "Synchronize repository releases with tags",
  98. Action: runRepoSyncReleases,
  99. }
  100. subcmdRegenerate = cli.Command{
  101. Name: "regenerate",
  102. Usage: "Regenerate specific files",
  103. Subcommands: []cli.Command{
  104. microcmdRegenHooks,
  105. microcmdRegenKeys,
  106. },
  107. }
  108. microcmdRegenHooks = cli.Command{
  109. Name: "hooks",
  110. Usage: "Regenerate git-hooks",
  111. Action: runRegenerateHooks,
  112. }
  113. microcmdRegenKeys = cli.Command{
  114. Name: "keys",
  115. Usage: "Regenerate authorized_keys file",
  116. Action: runRegenerateKeys,
  117. }
  118. subcmdAuth = cli.Command{
  119. Name: "auth",
  120. Usage: "Modify external auth providers",
  121. Subcommands: []cli.Command{
  122. microcmdAuthAddOauth,
  123. microcmdAuthUpdateOauth,
  124. cmdAuthAddLdapBindDn,
  125. cmdAuthUpdateLdapBindDn,
  126. cmdAuthAddLdapSimpleAuth,
  127. cmdAuthUpdateLdapSimpleAuth,
  128. microcmdAuthList,
  129. microcmdAuthDelete,
  130. },
  131. }
  132. microcmdAuthList = cli.Command{
  133. Name: "list",
  134. Usage: "List auth sources",
  135. Action: runListAuth,
  136. Flags: []cli.Flag{
  137. cli.IntFlag{
  138. Name: "min-width",
  139. Usage: "Minimal cell width including any padding for the formatted table",
  140. Value: 0,
  141. },
  142. cli.IntFlag{
  143. Name: "tab-width",
  144. Usage: "width of tab characters in formatted table (equivalent number of spaces)",
  145. Value: 8,
  146. },
  147. cli.IntFlag{
  148. Name: "padding",
  149. Usage: "padding added to a cell before computing its width",
  150. Value: 1,
  151. },
  152. cli.StringFlag{
  153. Name: "pad-char",
  154. Usage: `ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result)`,
  155. Value: "\t",
  156. },
  157. cli.BoolFlag{
  158. Name: "vertical-bars",
  159. Usage: "Set to true to print vertical bars between columns",
  160. },
  161. },
  162. }
  163. idFlag = cli.Int64Flag{
  164. Name: "id",
  165. Usage: "ID of authentication source",
  166. }
  167. microcmdAuthDelete = cli.Command{
  168. Name: "delete",
  169. Usage: "Delete specific auth source",
  170. Flags: []cli.Flag{idFlag},
  171. Action: runDeleteAuth,
  172. }
  173. oauthCLIFlags = []cli.Flag{
  174. cli.StringFlag{
  175. Name: "name",
  176. Value: "",
  177. Usage: "Application Name",
  178. },
  179. cli.StringFlag{
  180. Name: "provider",
  181. Value: "",
  182. Usage: "OAuth2 Provider",
  183. },
  184. cli.StringFlag{
  185. Name: "key",
  186. Value: "",
  187. Usage: "Client ID (Key)",
  188. },
  189. cli.StringFlag{
  190. Name: "secret",
  191. Value: "",
  192. Usage: "Client Secret",
  193. },
  194. cli.StringFlag{
  195. Name: "auto-discover-url",
  196. Value: "",
  197. Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)",
  198. },
  199. cli.StringFlag{
  200. Name: "use-custom-urls",
  201. Value: "false",
  202. Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints",
  203. },
  204. cli.StringFlag{
  205. Name: "custom-auth-url",
  206. Value: "",
  207. Usage: "Use a custom Authorization URL (option for GitLab/GitHub)",
  208. },
  209. cli.StringFlag{
  210. Name: "custom-token-url",
  211. Value: "",
  212. Usage: "Use a custom Token URL (option for GitLab/GitHub)",
  213. },
  214. cli.StringFlag{
  215. Name: "custom-profile-url",
  216. Value: "",
  217. Usage: "Use a custom Profile URL (option for GitLab/GitHub)",
  218. },
  219. cli.StringFlag{
  220. Name: "custom-email-url",
  221. Value: "",
  222. Usage: "Use a custom Email URL (option for GitHub)",
  223. },
  224. }
  225. microcmdAuthUpdateOauth = cli.Command{
  226. Name: "update-oauth",
  227. Usage: "Update existing Oauth authentication source",
  228. Action: runUpdateOauth,
  229. Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...),
  230. }
  231. microcmdAuthAddOauth = cli.Command{
  232. Name: "add-oauth",
  233. Usage: "Add new Oauth authentication source",
  234. Action: runAddOauth,
  235. Flags: oauthCLIFlags,
  236. }
  237. )
  238. func runChangePassword(c *cli.Context) error {
  239. if err := argsSet(c, "username", "password"); err != nil {
  240. return err
  241. }
  242. if err := initDB(); err != nil {
  243. return err
  244. }
  245. if !pwd.IsComplexEnough(c.String("password")) {
  246. return errors.New("Password does not meet complexity requirements")
  247. }
  248. uname := c.String("username")
  249. user, err := models.GetUserByName(uname)
  250. if err != nil {
  251. return err
  252. }
  253. if user.Salt, err = models.GetUserSalt(); err != nil {
  254. return err
  255. }
  256. user.HashPassword(c.String("password"))
  257. if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
  258. return err
  259. }
  260. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  261. return nil
  262. }
  263. func runCreateUser(c *cli.Context) error {
  264. if err := argsSet(c, "email"); err != nil {
  265. return err
  266. }
  267. if c.IsSet("name") && c.IsSet("username") {
  268. return errors.New("Cannot set both --name and --username flags")
  269. }
  270. if !c.IsSet("name") && !c.IsSet("username") {
  271. return errors.New("One of --name or --username flags must be set")
  272. }
  273. if c.IsSet("password") && c.IsSet("random-password") {
  274. return errors.New("cannot set both -random-password and -password flags")
  275. }
  276. var username string
  277. if c.IsSet("username") {
  278. username = c.String("username")
  279. } else {
  280. username = c.String("name")
  281. fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n")
  282. }
  283. if err := initDB(); err != nil {
  284. return err
  285. }
  286. var password string
  287. if c.IsSet("password") {
  288. password = c.String("password")
  289. } else if c.IsSet("random-password") {
  290. var err error
  291. password, err = pwd.Generate(c.Int("random-password-length"))
  292. if err != nil {
  293. return err
  294. }
  295. fmt.Printf("generated random password is '%s'\n", password)
  296. } else {
  297. return errors.New("must set either password or random-password flag")
  298. }
  299. // always default to true
  300. var changePassword = true
  301. // If this is the first user being created.
  302. // Take it as the admin and don't force a password update.
  303. if n := models.CountUsers(); n == 0 {
  304. changePassword = false
  305. }
  306. if c.IsSet("must-change-password") {
  307. changePassword = c.Bool("must-change-password")
  308. }
  309. u := &models.User{
  310. Name: username,
  311. Email: c.String("email"),
  312. Passwd: password,
  313. IsActive: true,
  314. IsAdmin: c.Bool("admin"),
  315. MustChangePassword: changePassword,
  316. Theme: setting.UI.DefaultTheme,
  317. }
  318. if err := models.CreateUser(u); err != nil {
  319. return fmt.Errorf("CreateUser: %v", err)
  320. }
  321. if c.Bool("access-token") {
  322. t := &models.AccessToken{
  323. Name: "gitea-admin",
  324. UID: u.ID,
  325. }
  326. if err := models.NewAccessToken(t); err != nil {
  327. return err
  328. }
  329. fmt.Printf("Access token was successfully created... %s\n", t.Token)
  330. }
  331. fmt.Printf("New user '%s' has been successfully created!\n", username)
  332. return nil
  333. }
  334. func runRepoSyncReleases(c *cli.Context) error {
  335. if err := initDB(); err != nil {
  336. return err
  337. }
  338. log.Trace("Synchronizing repository releases (this may take a while)")
  339. for page := 1; ; page++ {
  340. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  341. ListOptions: models.ListOptions{
  342. PageSize: models.RepositoryListDefaultPageSize,
  343. Page: page,
  344. },
  345. Private: true,
  346. })
  347. if err != nil {
  348. return fmt.Errorf("SearchRepositoryByName: %v", err)
  349. }
  350. if len(repos) == 0 {
  351. break
  352. }
  353. log.Trace("Processing next %d repos of %d", len(repos), count)
  354. for _, repo := range repos {
  355. log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
  356. gitRepo, err := git.OpenRepository(repo.RepoPath())
  357. if err != nil {
  358. log.Warn("OpenRepository: %v", err)
  359. continue
  360. }
  361. oldnum, err := getReleaseCount(repo.ID)
  362. if err != nil {
  363. log.Warn(" GetReleaseCountByRepoID: %v", err)
  364. }
  365. log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
  366. if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
  367. log.Warn(" SyncReleasesWithTags: %v", err)
  368. gitRepo.Close()
  369. continue
  370. }
  371. count, err = getReleaseCount(repo.ID)
  372. if err != nil {
  373. log.Warn(" GetReleaseCountByRepoID: %v", err)
  374. gitRepo.Close()
  375. continue
  376. }
  377. log.Trace(" repo %s releases synchronized to tags: from %d to %d",
  378. repo.FullName(), oldnum, count)
  379. gitRepo.Close()
  380. }
  381. }
  382. return nil
  383. }
  384. func getReleaseCount(id int64) (int64, error) {
  385. return models.GetReleaseCountByRepoID(
  386. id,
  387. models.FindReleasesOptions{
  388. IncludeTags: true,
  389. },
  390. )
  391. }
  392. func runRegenerateHooks(c *cli.Context) error {
  393. if err := initDB(); err != nil {
  394. return err
  395. }
  396. return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
  397. }
  398. func runRegenerateKeys(c *cli.Context) error {
  399. if err := initDB(); err != nil {
  400. return err
  401. }
  402. return models.RewriteAllPublicKeys()
  403. }
  404. func parseOAuth2Config(c *cli.Context) *models.OAuth2Config {
  405. var customURLMapping *oauth2.CustomURLMapping
  406. if c.IsSet("use-custom-urls") {
  407. customURLMapping = &oauth2.CustomURLMapping{
  408. TokenURL: c.String("custom-token-url"),
  409. AuthURL: c.String("custom-auth-url"),
  410. ProfileURL: c.String("custom-profile-url"),
  411. EmailURL: c.String("custom-email-url"),
  412. }
  413. } else {
  414. customURLMapping = nil
  415. }
  416. return &models.OAuth2Config{
  417. Provider: c.String("provider"),
  418. ClientID: c.String("key"),
  419. ClientSecret: c.String("secret"),
  420. OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
  421. CustomURLMapping: customURLMapping,
  422. }
  423. }
  424. func runAddOauth(c *cli.Context) error {
  425. if err := initDB(); err != nil {
  426. return err
  427. }
  428. return models.CreateLoginSource(&models.LoginSource{
  429. Type: models.LoginOAuth2,
  430. Name: c.String("name"),
  431. IsActived: true,
  432. Cfg: parseOAuth2Config(c),
  433. })
  434. }
  435. func runUpdateOauth(c *cli.Context) error {
  436. if !c.IsSet("id") {
  437. return fmt.Errorf("--id flag is missing")
  438. }
  439. if err := initDB(); err != nil {
  440. return err
  441. }
  442. source, err := models.GetLoginSourceByID(c.Int64("id"))
  443. if err != nil {
  444. return err
  445. }
  446. oAuth2Config := source.OAuth2()
  447. if c.IsSet("name") {
  448. source.Name = c.String("name")
  449. }
  450. if c.IsSet("provider") {
  451. oAuth2Config.Provider = c.String("provider")
  452. }
  453. if c.IsSet("key") {
  454. oAuth2Config.ClientID = c.String("key")
  455. }
  456. if c.IsSet("secret") {
  457. oAuth2Config.ClientSecret = c.String("secret")
  458. }
  459. if c.IsSet("auto-discover-url") {
  460. oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url")
  461. }
  462. // update custom URL mapping
  463. var customURLMapping = &oauth2.CustomURLMapping{}
  464. if oAuth2Config.CustomURLMapping != nil {
  465. customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
  466. customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL
  467. customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL
  468. customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL
  469. }
  470. if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") {
  471. customURLMapping.TokenURL = c.String("custom-token-url")
  472. }
  473. if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") {
  474. customURLMapping.AuthURL = c.String("custom-auth-url")
  475. }
  476. if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") {
  477. customURLMapping.ProfileURL = c.String("custom-profile-url")
  478. }
  479. if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") {
  480. customURLMapping.EmailURL = c.String("custom-email-url")
  481. }
  482. oAuth2Config.CustomURLMapping = customURLMapping
  483. source.Cfg = oAuth2Config
  484. return models.UpdateSource(source)
  485. }
  486. func runListAuth(c *cli.Context) error {
  487. if err := initDB(); err != nil {
  488. return err
  489. }
  490. loginSources, err := models.LoginSources()
  491. if err != nil {
  492. return err
  493. }
  494. flags := tabwriter.AlignRight
  495. if c.Bool("vertical-bars") {
  496. flags |= tabwriter.Debug
  497. }
  498. padChar := byte('\t')
  499. if len(c.String("pad-char")) > 0 {
  500. padChar = c.String("pad-char")[0]
  501. }
  502. // loop through each source and print
  503. w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
  504. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  505. for _, source := range loginSources {
  506. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, models.LoginNames[source.Type], source.IsActived)
  507. }
  508. w.Flush()
  509. return nil
  510. }
  511. func runDeleteAuth(c *cli.Context) error {
  512. if !c.IsSet("id") {
  513. return fmt.Errorf("--id flag is missing")
  514. }
  515. if err := initDB(); err != nil {
  516. return err
  517. }
  518. source, err := models.GetLoginSourceByID(c.Int64("id"))
  519. if err != nil {
  520. return err
  521. }
  522. return models.DeleteSource(source)
  523. }