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.

564 lines
13 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. }
  137. idFlag = cli.Int64Flag{
  138. Name: "id",
  139. Usage: "ID of authentication source",
  140. }
  141. microcmdAuthDelete = cli.Command{
  142. Name: "delete",
  143. Usage: "Delete specific auth source",
  144. Flags: []cli.Flag{idFlag},
  145. Action: runDeleteAuth,
  146. }
  147. oauthCLIFlags = []cli.Flag{
  148. cli.StringFlag{
  149. Name: "name",
  150. Value: "",
  151. Usage: "Application Name",
  152. },
  153. cli.StringFlag{
  154. Name: "provider",
  155. Value: "",
  156. Usage: "OAuth2 Provider",
  157. },
  158. cli.StringFlag{
  159. Name: "key",
  160. Value: "",
  161. Usage: "Client ID (Key)",
  162. },
  163. cli.StringFlag{
  164. Name: "secret",
  165. Value: "",
  166. Usage: "Client Secret",
  167. },
  168. cli.StringFlag{
  169. Name: "auto-discover-url",
  170. Value: "",
  171. Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)",
  172. },
  173. cli.StringFlag{
  174. Name: "use-custom-urls",
  175. Value: "false",
  176. Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints",
  177. },
  178. cli.StringFlag{
  179. Name: "custom-auth-url",
  180. Value: "",
  181. Usage: "Use a custom Authorization URL (option for GitLab/GitHub)",
  182. },
  183. cli.StringFlag{
  184. Name: "custom-token-url",
  185. Value: "",
  186. Usage: "Use a custom Token URL (option for GitLab/GitHub)",
  187. },
  188. cli.StringFlag{
  189. Name: "custom-profile-url",
  190. Value: "",
  191. Usage: "Use a custom Profile URL (option for GitLab/GitHub)",
  192. },
  193. cli.StringFlag{
  194. Name: "custom-email-url",
  195. Value: "",
  196. Usage: "Use a custom Email URL (option for GitHub)",
  197. },
  198. }
  199. microcmdAuthUpdateOauth = cli.Command{
  200. Name: "update-oauth",
  201. Usage: "Update existing Oauth authentication source",
  202. Action: runUpdateOauth,
  203. Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...),
  204. }
  205. microcmdAuthAddOauth = cli.Command{
  206. Name: "add-oauth",
  207. Usage: "Add new Oauth authentication source",
  208. Action: runAddOauth,
  209. Flags: oauthCLIFlags,
  210. }
  211. )
  212. func runChangePassword(c *cli.Context) error {
  213. if err := argsSet(c, "username", "password"); err != nil {
  214. return err
  215. }
  216. if err := initDB(); err != nil {
  217. return err
  218. }
  219. if !pwd.IsComplexEnough(c.String("password")) {
  220. return errors.New("Password does not meet complexity requirements")
  221. }
  222. uname := c.String("username")
  223. user, err := models.GetUserByName(uname)
  224. if err != nil {
  225. return err
  226. }
  227. if user.Salt, err = models.GetUserSalt(); err != nil {
  228. return err
  229. }
  230. user.HashPassword(c.String("password"))
  231. if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
  232. return err
  233. }
  234. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  235. return nil
  236. }
  237. func runCreateUser(c *cli.Context) error {
  238. if err := argsSet(c, "email"); err != nil {
  239. return err
  240. }
  241. if c.IsSet("name") && c.IsSet("username") {
  242. return errors.New("Cannot set both --name and --username flags")
  243. }
  244. if !c.IsSet("name") && !c.IsSet("username") {
  245. return errors.New("One of --name or --username flags must be set")
  246. }
  247. if c.IsSet("password") && c.IsSet("random-password") {
  248. return errors.New("cannot set both -random-password and -password flags")
  249. }
  250. var username string
  251. if c.IsSet("username") {
  252. username = c.String("username")
  253. } else {
  254. username = c.String("name")
  255. fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n")
  256. }
  257. if err := initDB(); err != nil {
  258. return err
  259. }
  260. var password string
  261. if c.IsSet("password") {
  262. password = c.String("password")
  263. } else if c.IsSet("random-password") {
  264. var err error
  265. password, err = pwd.Generate(c.Int("random-password-length"))
  266. if err != nil {
  267. return err
  268. }
  269. fmt.Printf("generated random password is '%s'\n", password)
  270. } else {
  271. return errors.New("must set either password or random-password flag")
  272. }
  273. // always default to true
  274. var changePassword = true
  275. // If this is the first user being created.
  276. // Take it as the admin and don't force a password update.
  277. if n := models.CountUsers(); n == 0 {
  278. changePassword = false
  279. }
  280. if c.IsSet("must-change-password") {
  281. changePassword = c.Bool("must-change-password")
  282. }
  283. u := &models.User{
  284. Name: username,
  285. Email: c.String("email"),
  286. Passwd: password,
  287. IsActive: true,
  288. IsAdmin: c.Bool("admin"),
  289. MustChangePassword: changePassword,
  290. Theme: setting.UI.DefaultTheme,
  291. }
  292. if err := models.CreateUser(u); err != nil {
  293. return fmt.Errorf("CreateUser: %v", err)
  294. }
  295. if c.Bool("access-token") {
  296. t := &models.AccessToken{
  297. Name: "gitea-admin",
  298. UID: u.ID,
  299. }
  300. if err := models.NewAccessToken(t); err != nil {
  301. return err
  302. }
  303. fmt.Printf("Access token was successfully created... %s\n", t.Token)
  304. }
  305. fmt.Printf("New user '%s' has been successfully created!\n", username)
  306. return nil
  307. }
  308. func runRepoSyncReleases(c *cli.Context) error {
  309. if err := initDB(); err != nil {
  310. return err
  311. }
  312. log.Trace("Synchronizing repository releases (this may take a while)")
  313. for page := 1; ; page++ {
  314. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  315. ListOptions: models.ListOptions{
  316. PageSize: models.RepositoryListDefaultPageSize,
  317. Page: page,
  318. },
  319. Private: true,
  320. })
  321. if err != nil {
  322. return fmt.Errorf("SearchRepositoryByName: %v", err)
  323. }
  324. if len(repos) == 0 {
  325. break
  326. }
  327. log.Trace("Processing next %d repos of %d", len(repos), count)
  328. for _, repo := range repos {
  329. log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
  330. gitRepo, err := git.OpenRepository(repo.RepoPath())
  331. if err != nil {
  332. log.Warn("OpenRepository: %v", err)
  333. continue
  334. }
  335. oldnum, err := getReleaseCount(repo.ID)
  336. if err != nil {
  337. log.Warn(" GetReleaseCountByRepoID: %v", err)
  338. }
  339. log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
  340. if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
  341. log.Warn(" SyncReleasesWithTags: %v", err)
  342. gitRepo.Close()
  343. continue
  344. }
  345. count, err = getReleaseCount(repo.ID)
  346. if err != nil {
  347. log.Warn(" GetReleaseCountByRepoID: %v", err)
  348. gitRepo.Close()
  349. continue
  350. }
  351. log.Trace(" repo %s releases synchronized to tags: from %d to %d",
  352. repo.FullName(), oldnum, count)
  353. gitRepo.Close()
  354. }
  355. }
  356. return nil
  357. }
  358. func getReleaseCount(id int64) (int64, error) {
  359. return models.GetReleaseCountByRepoID(
  360. id,
  361. models.FindReleasesOptions{
  362. IncludeTags: true,
  363. },
  364. )
  365. }
  366. func runRegenerateHooks(c *cli.Context) error {
  367. if err := initDB(); err != nil {
  368. return err
  369. }
  370. return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
  371. }
  372. func runRegenerateKeys(c *cli.Context) error {
  373. if err := initDB(); err != nil {
  374. return err
  375. }
  376. return models.RewriteAllPublicKeys()
  377. }
  378. func parseOAuth2Config(c *cli.Context) *models.OAuth2Config {
  379. var customURLMapping *oauth2.CustomURLMapping
  380. if c.IsSet("use-custom-urls") {
  381. customURLMapping = &oauth2.CustomURLMapping{
  382. TokenURL: c.String("custom-token-url"),
  383. AuthURL: c.String("custom-auth-url"),
  384. ProfileURL: c.String("custom-profile-url"),
  385. EmailURL: c.String("custom-email-url"),
  386. }
  387. } else {
  388. customURLMapping = nil
  389. }
  390. return &models.OAuth2Config{
  391. Provider: c.String("provider"),
  392. ClientID: c.String("key"),
  393. ClientSecret: c.String("secret"),
  394. OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
  395. CustomURLMapping: customURLMapping,
  396. }
  397. }
  398. func runAddOauth(c *cli.Context) error {
  399. if err := initDB(); err != nil {
  400. return err
  401. }
  402. return models.CreateLoginSource(&models.LoginSource{
  403. Type: models.LoginOAuth2,
  404. Name: c.String("name"),
  405. IsActived: true,
  406. Cfg: parseOAuth2Config(c),
  407. })
  408. }
  409. func runUpdateOauth(c *cli.Context) error {
  410. if !c.IsSet("id") {
  411. return fmt.Errorf("--id flag is missing")
  412. }
  413. if err := initDB(); err != nil {
  414. return err
  415. }
  416. source, err := models.GetLoginSourceByID(c.Int64("id"))
  417. if err != nil {
  418. return err
  419. }
  420. oAuth2Config := source.OAuth2()
  421. if c.IsSet("name") {
  422. source.Name = c.String("name")
  423. }
  424. if c.IsSet("provider") {
  425. oAuth2Config.Provider = c.String("provider")
  426. }
  427. if c.IsSet("key") {
  428. oAuth2Config.ClientID = c.String("key")
  429. }
  430. if c.IsSet("secret") {
  431. oAuth2Config.ClientSecret = c.String("secret")
  432. }
  433. if c.IsSet("auto-discover-url") {
  434. oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url")
  435. }
  436. // update custom URL mapping
  437. var customURLMapping = &oauth2.CustomURLMapping{}
  438. if oAuth2Config.CustomURLMapping != nil {
  439. customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
  440. customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL
  441. customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL
  442. customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL
  443. }
  444. if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") {
  445. customURLMapping.TokenURL = c.String("custom-token-url")
  446. }
  447. if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") {
  448. customURLMapping.AuthURL = c.String("custom-auth-url")
  449. }
  450. if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") {
  451. customURLMapping.ProfileURL = c.String("custom-profile-url")
  452. }
  453. if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") {
  454. customURLMapping.EmailURL = c.String("custom-email-url")
  455. }
  456. oAuth2Config.CustomURLMapping = customURLMapping
  457. source.Cfg = oAuth2Config
  458. return models.UpdateSource(source)
  459. }
  460. func runListAuth(c *cli.Context) error {
  461. if err := initDB(); err != nil {
  462. return err
  463. }
  464. loginSources, err := models.LoginSources()
  465. if err != nil {
  466. return err
  467. }
  468. // loop through each source and print
  469. w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
  470. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  471. for _, source := range loginSources {
  472. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, models.LoginNames[source.Type], source.IsActived)
  473. }
  474. w.Flush()
  475. return nil
  476. }
  477. func runDeleteAuth(c *cli.Context) error {
  478. if !c.IsSet("id") {
  479. return fmt.Errorf("--id flag is missing")
  480. }
  481. if err := initDB(); err != nil {
  482. return err
  483. }
  484. source, err := models.GetLoginSourceByID(c.Int64("id"))
  485. if err != nil {
  486. return err
  487. }
  488. return models.DeleteSource(source)
  489. }