idp.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package sqlite
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. storepb "github.com/usememos/memos/proto/gen/store"
  7. "github.com/usememos/memos/store"
  8. )
  9. func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
  10. placeholders := []string{"?", "?", "?", "?"}
  11. fields := []string{"`name`", "`type`", "`identifier_filter`", "`config`"}
  12. args := []any{create.Name, create.Type.String(), create.IdentifierFilter, create.Config}
  13. stmt := "INSERT INTO `idp` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholders, ", ") + ") RETURNING `id`"
  14. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID); err != nil {
  15. return nil, err
  16. }
  17. identityProvider := create
  18. return identityProvider, nil
  19. }
  20. func (d *DB) ListIdentityProviders(ctx context.Context, find *store.FindIdentityProvider) ([]*store.IdentityProvider, error) {
  21. where, args := []string{"1 = 1"}, []any{}
  22. if v := find.ID; v != nil {
  23. where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
  24. }
  25. rows, err := d.db.QueryContext(ctx, `
  26. SELECT
  27. id,
  28. name,
  29. type,
  30. identifier_filter,
  31. config
  32. FROM idp
  33. WHERE `+strings.Join(where, " AND ")+` ORDER BY id ASC`,
  34. args...,
  35. )
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer rows.Close()
  40. var identityProviders []*store.IdentityProvider
  41. for rows.Next() {
  42. var identityProvider store.IdentityProvider
  43. var typeString string
  44. if err := rows.Scan(
  45. &identityProvider.ID,
  46. &identityProvider.Name,
  47. &typeString,
  48. &identityProvider.IdentifierFilter,
  49. &identityProvider.Config,
  50. ); err != nil {
  51. return nil, err
  52. }
  53. identityProvider.Type = storepb.IdentityProvider_Type(storepb.IdentityProvider_Type_value[typeString])
  54. identityProviders = append(identityProviders, &identityProvider)
  55. }
  56. if err := rows.Err(); err != nil {
  57. return nil, err
  58. }
  59. return identityProviders, nil
  60. }
  61. func (d *DB) UpdateIdentityProvider(ctx context.Context, update *store.UpdateIdentityProvider) (*store.IdentityProvider, error) {
  62. set, args := []string{}, []any{}
  63. if v := update.Name; v != nil {
  64. set, args = append(set, "name = ?"), append(args, *v)
  65. }
  66. if v := update.IdentifierFilter; v != nil {
  67. set, args = append(set, "identifier_filter = ?"), append(args, *v)
  68. }
  69. if v := update.Config; v != nil {
  70. set, args = append(set, "config = ?"), append(args, *v)
  71. }
  72. args = append(args, update.ID)
  73. stmt := `
  74. UPDATE idp
  75. SET ` + strings.Join(set, ", ") + `
  76. WHERE id = ?
  77. RETURNING id, name, type, identifier_filter, config
  78. `
  79. var identityProvider store.IdentityProvider
  80. var typeString string
  81. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  82. &identityProvider.ID,
  83. &identityProvider.Name,
  84. &typeString,
  85. &identityProvider.IdentifierFilter,
  86. &identityProvider.Config,
  87. ); err != nil {
  88. return nil, err
  89. }
  90. identityProvider.Type = storepb.IdentityProvider_Type(storepb.IdentityProvider_Type_value[typeString])
  91. return &identityProvider, nil
  92. }
  93. func (d *DB) DeleteIdentityProvider(ctx context.Context, delete *store.DeleteIdentityProvider) error {
  94. where, args := []string{"id = ?"}, []any{delete.ID}
  95. stmt := `DELETE FROM idp WHERE ` + strings.Join(where, " AND ")
  96. result, err := d.db.ExecContext(ctx, stmt, args...)
  97. if err != nil {
  98. return err
  99. }
  100. if _, err = result.RowsAffected(); err != nil {
  101. return err
  102. }
  103. return nil
  104. }