diff --git a/app/author_service.go b/app/author_service.go index 93d52c8..362d6d9 100644 --- a/app/author_service.go +++ b/app/author_service.go @@ -36,6 +36,21 @@ func (s *AuthorService) Create(name string, password string) (*model.Author, err return s.repo.Create(name, hash) } +func (s *AuthorService) SetPassword(name string, password string) error { + hash, err := hashPassword(password) + if err != nil { + return err + } + author, err := s.repo.FindByName(name) + if err != nil { + return err + } + author.PasswordHash = hash + err = s.repo.Update(author) + return err + +} + func (s *AuthorService) FindByName(name string) (*model.Author, error) { return s.repo.FindByName(name) } diff --git a/app/repository/interfaces.go b/app/repository/interfaces.go index 262ed0e..d79478e 100644 --- a/app/repository/interfaces.go +++ b/app/repository/interfaces.go @@ -27,6 +27,8 @@ type AuthorRepository interface { // Create creates a new author // It returns an error if the name is already taken Create(name string, passwordHash string) (*model.Author, error) + + Update(author *model.Author) error // FindByName finds an author by name // It returns an error if the author is not found FindByName(name string) (*model.Author, error) diff --git a/app/webmention_test.go b/app/webmention_test.go index a7a139d..9ee6592 100644 --- a/app/webmention_test.go +++ b/app/webmention_test.go @@ -56,11 +56,13 @@ func getWebmentionService() *app.WebmentionService { interactionRepo := infra.NewInteractionRepo(db, interactionRegister) + configRepo := infra.NewConfigRepo(db) + bus := app.NewEventBus() http := infra.OwlHttpClient{} return app.NewWebmentionService( - interactionRepo, entryRepo, &http, bus, + configRepo, interactionRepo, entryRepo, &http, bus, ) } diff --git a/cmd/owl/reset_password.go b/cmd/owl/reset_password.go new file mode 100644 index 0000000..b984110 --- /dev/null +++ b/cmd/owl/reset_password.go @@ -0,0 +1,26 @@ +package main + +import ( + "owl-blogs/infra" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(resetPasswordCmd) + + resetPasswordCmd.Flags().StringVarP(&user, "user", "u", "", "The user name") + resetPasswordCmd.MarkFlagRequired("user") + resetPasswordCmd.Flags().StringVarP(&password, "password", "p", "", "The new password") + resetPasswordCmd.MarkFlagRequired("password") +} + +var resetPasswordCmd = &cobra.Command{ + Use: "reset-password", + Short: "Resets the password of an author", + Long: `Resets the password of an author`, + Run: func(cmd *cobra.Command, args []string) { + db := infra.NewSqliteDB(DbPath) + App(db).AuthorService.Create(user, password) + }, +} diff --git a/infra/author_repository.go b/infra/author_repository.go index 0c7bd98..6cac6d1 100644 --- a/infra/author_repository.go +++ b/infra/author_repository.go @@ -59,3 +59,12 @@ func (r *DefaultAuthorRepo) Create(name string, passwordHash string) (*model.Aut PasswordHash: author.PasswordHash, }, nil } + +func (r *DefaultAuthorRepo) Update(author *model.Author) error { + sqlA := sqlAuthor{ + Name: author.Name, + PasswordHash: author.PasswordHash, + } + _, err := r.db.NamedExec("UPDATE authors SET password_hash = :password_hash WHERE name = :name", sqlA) + return err +} diff --git a/infra/author_repository_test.go b/infra/author_repository_test.go index 7283ca8..0ef50fc 100644 --- a/infra/author_repository_test.go +++ b/infra/author_repository_test.go @@ -45,3 +45,19 @@ func TestAuthorRepoNoSideEffect(t *testing.T) { require.Equal(t, author2.Name, "name2") require.Equal(t, author2.PasswordHash, "password2") } + +func TestAuthorUpdate(t *testing.T) { + repo := setupAutherRepo() + + author, err := repo.Create("name1", "password1") + require.NoError(t, err) + + author.PasswordHash = "password2" + err = repo.Update(author) + require.NoError(t, err) + + author, err = repo.FindByName("name1") + require.NoError(t, err) + require.Equal(t, author.PasswordHash, "password2") + +}