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

admin.go 17 kB

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>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. "context"
  8. "errors"
  9. "fmt"
  10. "os"
  11. "text/tabwriter"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/auth/oauth2"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/graceful"
  16. "code.gitea.io/gitea/modules/log"
  17. pwd "code.gitea.io/gitea/modules/password"
  18. repo_module "code.gitea.io/gitea/modules/repository"
  19. "code.gitea.io/gitea/modules/setting"
  20. "github.com/urfave/cli"
  21. )
  22. var (
  23. // CmdAdmin represents the available admin sub-command.
  24. CmdAdmin = cli.Command{
  25. Name: "admin",
  26. Usage: "Command line interface to perform common administrative operations",
  27. Subcommands: []cli.Command{
  28. subcmdUser,
  29. subcmdRepoSyncReleases,
  30. subcmdRegenerate,
  31. subcmdAuth,
  32. subcmdSendMail,
  33. },
  34. }
  35. subcmdUser = cli.Command{
  36. Name: "user",
  37. Usage: "Modify users",
  38. Subcommands: []cli.Command{
  39. microcmdUserCreate,
  40. microcmdUserList,
  41. microcmdUserChangePassword,
  42. microcmdUserDelete,
  43. },
  44. }
  45. microcmdUserList = cli.Command{
  46. Name: "list",
  47. Usage: "List users",
  48. Action: runListUsers,
  49. Flags: []cli.Flag{
  50. cli.BoolFlag{
  51. Name: "admin",
  52. Usage: "List only admin users",
  53. },
  54. },
  55. }
  56. microcmdUserCreate = cli.Command{
  57. Name: "create",
  58. Usage: "Create a new user in database",
  59. Action: runCreateUser,
  60. Flags: []cli.Flag{
  61. cli.StringFlag{
  62. Name: "name",
  63. Usage: "Username. DEPRECATED: use username instead",
  64. },
  65. cli.StringFlag{
  66. Name: "username",
  67. Usage: "Username",
  68. },
  69. cli.StringFlag{
  70. Name: "password",
  71. Usage: "User password",
  72. },
  73. cli.StringFlag{
  74. Name: "email",
  75. Usage: "User email address",
  76. },
  77. cli.BoolFlag{
  78. Name: "admin",
  79. Usage: "User is an admin",
  80. },
  81. cli.BoolFlag{
  82. Name: "random-password",
  83. Usage: "Generate a random password for the user",
  84. },
  85. cli.BoolFlag{
  86. Name: "must-change-password",
  87. Usage: "Set this option to false to prevent forcing the user to change their password after initial login, (Default: true)",
  88. },
  89. cli.IntFlag{
  90. Name: "random-password-length",
  91. Usage: "Length of the random password to be generated",
  92. Value: 12,
  93. },
  94. cli.BoolFlag{
  95. Name: "access-token",
  96. Usage: "Generate access token for the user",
  97. },
  98. },
  99. }
  100. microcmdUserChangePassword = cli.Command{
  101. Name: "change-password",
  102. Usage: "Change a user's password",
  103. Action: runChangePassword,
  104. Flags: []cli.Flag{
  105. cli.StringFlag{
  106. Name: "username,u",
  107. Value: "",
  108. Usage: "The user to change password for",
  109. },
  110. cli.StringFlag{
  111. Name: "password,p",
  112. Value: "",
  113. Usage: "New password to set for user",
  114. },
  115. },
  116. }
  117. microcmdUserDelete = cli.Command{
  118. Name: "delete",
  119. Usage: "Delete specific user",
  120. Flags: []cli.Flag{idFlag},
  121. Action: runDeleteUser,
  122. }
  123. subcmdRepoSyncReleases = cli.Command{
  124. Name: "repo-sync-releases",
  125. Usage: "Synchronize repository releases with tags",
  126. Action: runRepoSyncReleases,
  127. }
  128. subcmdRegenerate = cli.Command{
  129. Name: "regenerate",
  130. Usage: "Regenerate specific files",
  131. Subcommands: []cli.Command{
  132. microcmdRegenHooks,
  133. microcmdRegenKeys,
  134. },
  135. }
  136. microcmdRegenHooks = cli.Command{
  137. Name: "hooks",
  138. Usage: "Regenerate git-hooks",
  139. Action: runRegenerateHooks,
  140. }
  141. microcmdRegenKeys = cli.Command{
  142. Name: "keys",
  143. Usage: "Regenerate authorized_keys file",
  144. Action: runRegenerateKeys,
  145. }
  146. subcmdAuth = cli.Command{
  147. Name: "auth",
  148. Usage: "Modify external auth providers",
  149. Subcommands: []cli.Command{
  150. microcmdAuthAddOauth,
  151. microcmdAuthUpdateOauth,
  152. cmdAuthAddLdapBindDn,
  153. cmdAuthUpdateLdapBindDn,
  154. cmdAuthAddLdapSimpleAuth,
  155. cmdAuthUpdateLdapSimpleAuth,
  156. microcmdAuthList,
  157. microcmdAuthDelete,
  158. },
  159. }
  160. microcmdAuthList = cli.Command{
  161. Name: "list",
  162. Usage: "List auth sources",
  163. Action: runListAuth,
  164. Flags: []cli.Flag{
  165. cli.IntFlag{
  166. Name: "min-width",
  167. Usage: "Minimal cell width including any padding for the formatted table",
  168. Value: 0,
  169. },
  170. cli.IntFlag{
  171. Name: "tab-width",
  172. Usage: "width of tab characters in formatted table (equivalent number of spaces)",
  173. Value: 8,
  174. },
  175. cli.IntFlag{
  176. Name: "padding",
  177. Usage: "padding added to a cell before computing its width",
  178. Value: 1,
  179. },
  180. cli.StringFlag{
  181. Name: "pad-char",
  182. 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)`,
  183. Value: "\t",
  184. },
  185. cli.BoolFlag{
  186. Name: "vertical-bars",
  187. Usage: "Set to true to print vertical bars between columns",
  188. },
  189. },
  190. }
  191. idFlag = cli.Int64Flag{
  192. Name: "id",
  193. Usage: "ID of authentication source",
  194. }
  195. microcmdAuthDelete = cli.Command{
  196. Name: "delete",
  197. Usage: "Delete specific auth source",
  198. Flags: []cli.Flag{idFlag},
  199. Action: runDeleteAuth,
  200. }
  201. oauthCLIFlags = []cli.Flag{
  202. cli.StringFlag{
  203. Name: "name",
  204. Value: "",
  205. Usage: "Application Name",
  206. },
  207. cli.StringFlag{
  208. Name: "provider",
  209. Value: "",
  210. Usage: "OAuth2 Provider",
  211. },
  212. cli.StringFlag{
  213. Name: "key",
  214. Value: "",
  215. Usage: "Client ID (Key)",
  216. },
  217. cli.StringFlag{
  218. Name: "secret",
  219. Value: "",
  220. Usage: "Client Secret",
  221. },
  222. cli.StringFlag{
  223. Name: "auto-discover-url",
  224. Value: "",
  225. Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)",
  226. },
  227. cli.StringFlag{
  228. Name: "use-custom-urls",
  229. Value: "false",
  230. Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints",
  231. },
  232. cli.StringFlag{
  233. Name: "custom-auth-url",
  234. Value: "",
  235. Usage: "Use a custom Authorization URL (option for GitLab/GitHub)",
  236. },
  237. cli.StringFlag{
  238. Name: "custom-token-url",
  239. Value: "",
  240. Usage: "Use a custom Token URL (option for GitLab/GitHub)",
  241. },
  242. cli.StringFlag{
  243. Name: "custom-profile-url",
  244. Value: "",
  245. Usage: "Use a custom Profile URL (option for GitLab/GitHub)",
  246. },
  247. cli.StringFlag{
  248. Name: "custom-email-url",
  249. Value: "",
  250. Usage: "Use a custom Email URL (option for GitHub)",
  251. },
  252. }
  253. microcmdAuthUpdateOauth = cli.Command{
  254. Name: "update-oauth",
  255. Usage: "Update existing Oauth authentication source",
  256. Action: runUpdateOauth,
  257. Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...),
  258. }
  259. microcmdAuthAddOauth = cli.Command{
  260. Name: "add-oauth",
  261. Usage: "Add new Oauth authentication source",
  262. Action: runAddOauth,
  263. Flags: oauthCLIFlags,
  264. }
  265. subcmdSendMail = cli.Command{
  266. Name: "sendmail",
  267. Usage: "Send a message to all users",
  268. Action: runSendMail,
  269. Flags: []cli.Flag{
  270. cli.StringFlag{
  271. Name: "title",
  272. Usage: `a title of a message`,
  273. Value: "",
  274. },
  275. cli.StringFlag{
  276. Name: "content",
  277. Usage: "a content of a message",
  278. Value: "",
  279. },
  280. cli.BoolFlag{
  281. Name: "force,f",
  282. Usage: "A flag to bypass a confirmation step",
  283. },
  284. },
  285. }
  286. )
  287. func runChangePassword(c *cli.Context) error {
  288. if err := argsSet(c, "username", "password"); err != nil {
  289. return err
  290. }
  291. if err := initDB(); err != nil {
  292. return err
  293. }
  294. if !pwd.IsComplexEnough(c.String("password")) {
  295. return errors.New("Password does not meet complexity requirements")
  296. }
  297. pwned, err := pwd.IsPwned(context.Background(), c.String("password"))
  298. if err != nil {
  299. return err
  300. }
  301. if pwned {
  302. return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.\nFor more details, see https://haveibeenpwned.com/Passwords")
  303. }
  304. uname := c.String("username")
  305. user, err := models.GetUserByName(uname)
  306. if err != nil {
  307. return err
  308. }
  309. if user.Salt, err = models.GetUserSalt(); err != nil {
  310. return err
  311. }
  312. user.HashPassword(c.String("password"))
  313. if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
  314. return err
  315. }
  316. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  317. return nil
  318. }
  319. func runCreateUser(c *cli.Context) error {
  320. if err := argsSet(c, "email"); err != nil {
  321. return err
  322. }
  323. if c.IsSet("name") && c.IsSet("username") {
  324. return errors.New("Cannot set both --name and --username flags")
  325. }
  326. if !c.IsSet("name") && !c.IsSet("username") {
  327. return errors.New("One of --name or --username flags must be set")
  328. }
  329. if c.IsSet("password") && c.IsSet("random-password") {
  330. return errors.New("cannot set both -random-password and -password flags")
  331. }
  332. var username string
  333. if c.IsSet("username") {
  334. username = c.String("username")
  335. } else {
  336. username = c.String("name")
  337. fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n")
  338. }
  339. if err := initDB(); err != nil {
  340. return err
  341. }
  342. var password string
  343. if c.IsSet("password") {
  344. password = c.String("password")
  345. } else if c.IsSet("random-password") {
  346. var err error
  347. password, err = pwd.Generate(c.Int("random-password-length"))
  348. if err != nil {
  349. return err
  350. }
  351. fmt.Printf("generated random password is '%s'\n", password)
  352. } else {
  353. return errors.New("must set either password or random-password flag")
  354. }
  355. // always default to true
  356. var changePassword = true
  357. // If this is the first user being created.
  358. // Take it as the admin and don't force a password update.
  359. if n := models.CountUsers(); n == 0 {
  360. changePassword = false
  361. }
  362. if c.IsSet("must-change-password") {
  363. changePassword = c.Bool("must-change-password")
  364. }
  365. u := &models.User{
  366. Name: username,
  367. Email: c.String("email"),
  368. Passwd: password,
  369. IsActive: true,
  370. IsAdmin: c.Bool("admin"),
  371. MustChangePassword: changePassword,
  372. Theme: setting.UI.DefaultTheme,
  373. }
  374. if err := models.CreateUser(u); err != nil {
  375. return fmt.Errorf("CreateUser: %v", err)
  376. }
  377. if c.Bool("access-token") {
  378. t := &models.AccessToken{
  379. Name: "gitea-admin",
  380. UID: u.ID,
  381. }
  382. if err := models.NewAccessToken(t); err != nil {
  383. return err
  384. }
  385. fmt.Printf("Access token was successfully created... %s\n", t.Token)
  386. }
  387. fmt.Printf("New user '%s' has been successfully created!\n", username)
  388. return nil
  389. }
  390. func runListUsers(c *cli.Context) error {
  391. if err := initDB(); err != nil {
  392. return err
  393. }
  394. users, err := models.GetAllUsers()
  395. if err != nil {
  396. return err
  397. }
  398. w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
  399. if c.IsSet("admin") {
  400. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
  401. for _, u := range users {
  402. if u.IsAdmin {
  403. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
  404. }
  405. }
  406. } else {
  407. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\n")
  408. for _, u := range users {
  409. fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin)
  410. }
  411. }
  412. w.Flush()
  413. return nil
  414. }
  415. func runDeleteUser(c *cli.Context) error {
  416. if !c.IsSet("id") {
  417. return fmt.Errorf("--id flag is missing")
  418. }
  419. if err := initDB(); err != nil {
  420. return err
  421. }
  422. user, err := models.GetUserByID(c.Int64("id"))
  423. if err != nil {
  424. return err
  425. }
  426. return models.DeleteUser(user)
  427. }
  428. func runRepoSyncReleases(c *cli.Context) error {
  429. if err := initDB(); err != nil {
  430. return err
  431. }
  432. log.Trace("Synchronizing repository releases (this may take a while)")
  433. for page := 1; ; page++ {
  434. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  435. ListOptions: models.ListOptions{
  436. PageSize: models.RepositoryListDefaultPageSize,
  437. Page: page,
  438. },
  439. Private: true,
  440. })
  441. if err != nil {
  442. return fmt.Errorf("SearchRepositoryByName: %v", err)
  443. }
  444. if len(repos) == 0 {
  445. break
  446. }
  447. log.Trace("Processing next %d repos of %d", len(repos), count)
  448. for _, repo := range repos {
  449. log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
  450. gitRepo, err := git.OpenRepository(repo.RepoPath())
  451. if err != nil {
  452. log.Warn("OpenRepository: %v", err)
  453. continue
  454. }
  455. oldnum, err := getReleaseCount(repo.ID)
  456. if err != nil {
  457. log.Warn(" GetReleaseCountByRepoID: %v", err)
  458. }
  459. log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
  460. if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
  461. log.Warn(" SyncReleasesWithTags: %v", err)
  462. gitRepo.Close()
  463. continue
  464. }
  465. count, err = getReleaseCount(repo.ID)
  466. if err != nil {
  467. log.Warn(" GetReleaseCountByRepoID: %v", err)
  468. gitRepo.Close()
  469. continue
  470. }
  471. log.Trace(" repo %s releases synchronized to tags: from %d to %d",
  472. repo.FullName(), oldnum, count)
  473. gitRepo.Close()
  474. }
  475. }
  476. return nil
  477. }
  478. func getReleaseCount(id int64) (int64, error) {
  479. return models.GetReleaseCountByRepoID(
  480. id,
  481. models.FindReleasesOptions{
  482. IncludeTags: true,
  483. },
  484. )
  485. }
  486. func runRegenerateHooks(c *cli.Context) error {
  487. if err := initDB(); err != nil {
  488. return err
  489. }
  490. return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
  491. }
  492. func runRegenerateKeys(c *cli.Context) error {
  493. if err := initDB(); err != nil {
  494. return err
  495. }
  496. return models.RewriteAllPublicKeys()
  497. }
  498. func parseOAuth2Config(c *cli.Context) *models.OAuth2Config {
  499. var customURLMapping *oauth2.CustomURLMapping
  500. if c.IsSet("use-custom-urls") {
  501. customURLMapping = &oauth2.CustomURLMapping{
  502. TokenURL: c.String("custom-token-url"),
  503. AuthURL: c.String("custom-auth-url"),
  504. ProfileURL: c.String("custom-profile-url"),
  505. EmailURL: c.String("custom-email-url"),
  506. }
  507. } else {
  508. customURLMapping = nil
  509. }
  510. return &models.OAuth2Config{
  511. Provider: c.String("provider"),
  512. ClientID: c.String("key"),
  513. ClientSecret: c.String("secret"),
  514. OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
  515. CustomURLMapping: customURLMapping,
  516. }
  517. }
  518. func runAddOauth(c *cli.Context) error {
  519. if err := initDB(); err != nil {
  520. return err
  521. }
  522. return models.CreateLoginSource(&models.LoginSource{
  523. Type: models.LoginOAuth2,
  524. Name: c.String("name"),
  525. IsActived: true,
  526. Cfg: parseOAuth2Config(c),
  527. })
  528. }
  529. func runUpdateOauth(c *cli.Context) error {
  530. if !c.IsSet("id") {
  531. return fmt.Errorf("--id flag is missing")
  532. }
  533. if err := initDB(); err != nil {
  534. return err
  535. }
  536. source, err := models.GetLoginSourceByID(c.Int64("id"))
  537. if err != nil {
  538. return err
  539. }
  540. oAuth2Config := source.OAuth2()
  541. if c.IsSet("name") {
  542. source.Name = c.String("name")
  543. }
  544. if c.IsSet("provider") {
  545. oAuth2Config.Provider = c.String("provider")
  546. }
  547. if c.IsSet("key") {
  548. oAuth2Config.ClientID = c.String("key")
  549. }
  550. if c.IsSet("secret") {
  551. oAuth2Config.ClientSecret = c.String("secret")
  552. }
  553. if c.IsSet("auto-discover-url") {
  554. oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url")
  555. }
  556. // update custom URL mapping
  557. var customURLMapping = &oauth2.CustomURLMapping{}
  558. if oAuth2Config.CustomURLMapping != nil {
  559. customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
  560. customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL
  561. customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL
  562. customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL
  563. }
  564. if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") {
  565. customURLMapping.TokenURL = c.String("custom-token-url")
  566. }
  567. if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") {
  568. customURLMapping.AuthURL = c.String("custom-auth-url")
  569. }
  570. if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") {
  571. customURLMapping.ProfileURL = c.String("custom-profile-url")
  572. }
  573. if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") {
  574. customURLMapping.EmailURL = c.String("custom-email-url")
  575. }
  576. oAuth2Config.CustomURLMapping = customURLMapping
  577. source.Cfg = oAuth2Config
  578. return models.UpdateSource(source)
  579. }
  580. func runListAuth(c *cli.Context) error {
  581. if err := initDB(); err != nil {
  582. return err
  583. }
  584. loginSources, err := models.LoginSources()
  585. if err != nil {
  586. return err
  587. }
  588. flags := tabwriter.AlignRight
  589. if c.Bool("vertical-bars") {
  590. flags |= tabwriter.Debug
  591. }
  592. padChar := byte('\t')
  593. if len(c.String("pad-char")) > 0 {
  594. padChar = c.String("pad-char")[0]
  595. }
  596. // loop through each source and print
  597. w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
  598. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  599. for _, source := range loginSources {
  600. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, models.LoginNames[source.Type], source.IsActived)
  601. }
  602. w.Flush()
  603. return nil
  604. }
  605. func runDeleteAuth(c *cli.Context) error {
  606. if !c.IsSet("id") {
  607. return fmt.Errorf("--id flag is missing")
  608. }
  609. if err := initDB(); err != nil {
  610. return err
  611. }
  612. source, err := models.GetLoginSourceByID(c.Int64("id"))
  613. if err != nil {
  614. return err
  615. }
  616. return models.DeleteSource(source)
  617. }