Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent sending emails and notifications to inactive users #2384

Merged
merged 7 commits into from
Sep 16, 2017

Conversation

daviian
Copy link
Member

@daviian daviian commented Aug 24, 2017

Targets #2034

Added checks if user is inactive (is_active = false or prohibit_login = true) before creating notifications and sending mails.
Furthermore I added NOT NULL DEFAULT false to prohibit_login field.

Changed unit tests accordingly and added a migration, which I tested for mssql, mysql and postgresql.

if err != nil {
return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
}
if issue.PosterID != doer.ID && poster.IsActive == true {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not check booleans with == do just simple && poster.IsActive {. Should there be also check for && !poster.ProhibitLogin?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right. Will fix that.

)

func addDefaultValueToUserProhibitLogin(x *xorm.Engine) (err error) {
// Update user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not load data in go code. Just update with condition where prohibid_login is null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but there was an issue with one database.
Was complaining about not finding anything while updating because I had no user with nullin database.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it again with your proposed change.
It is working now, maybe I had a problem somewhere else ;-)

models/user.go Outdated
@@ -111,7 +111,7 @@ type User struct {
AllowGitHook bool
AllowImportLocal bool // Allow migrate repository by local path
AllowCreateOrganization bool `xorm:"DEFAULT true"`
ProhibitLogin bool
ProhibitLogin bool `xorm:"DEFAULT false"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add notnull here

return watches, e.Find(&watches, &Watch{RepoID: repoID})
return watches, e.Where("`watch`.repo_id=?", repoID).
And("`user`.is_active=?", true).
And("`user`.prohibit_login=?", false).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think user who is prohibit_login will be moved from watchers UI.

Copy link
Member Author

@daviian daviian Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method is also used in

watches, err := getWatchers(e, issue.RepoID)

Perhaps I should add a second method or a parameter to add restriction conditionally?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, add additional parameter to function to know in to return all as it was or just active ones

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunny @lafriks I figured out that the view isn't using this method anyways. The view is using the method from here https://github.com/daviian/gitea/blob/e3f679f39da7985193cec28a7c21fbc16ab2310d/models/repo_watch.go#L67

The method in question is only used by notification and mail

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunny Any response?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daviian OK.

models/issue.go Outdated
And("`comment`.type = ?", CommentTypeComment).
And("`user`.is_active = ?", true).
And("`user`.prohibit_login = ?", false).
Join("LEFT", "user", "`user`.id = `comment`.poster_id").
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we're doing a left join here. If the goal is to include comments that don't have a "valid" poster (e.g. deleted user), I don't think this will work; for those comments the user.is_active column will be NULL, and the user.is_active = true condition won't hold. And if we don't want to include such comments, why not just do an inner join?

Ditto elsewhere

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethantkoenig good point, I also think it should be changed to INNER join

@daviian
Copy link
Member Author

daviian commented Aug 28, 2017

I will implement the requested changes this week, in the best case this evening.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Aug 28, 2017
@daviian daviian force-pushed the inactive-user-receive-mail-fix branch from e3f679f to 4c0d946 Compare August 29, 2017 07:45
@lunny lunny added this to the 1.3.0 milestone Aug 30, 2017
@lunny lunny added the type/bug label Aug 30, 2017
@lunny
Copy link
Member

lunny commented Sep 1, 2017

@daviian have you fixed all points?

@daviian
Copy link
Member Author

daviian commented Sep 1, 2017

@lunny I have changed the joins to INNER as requested by @ethantkoenig and fixed mentioned mistakes from @lafriks
And I still wait for your answer on my comment regarding the impact on UI.

@lafriks
Copy link
Member

lafriks commented Sep 3, 2017

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Sep 3, 2017
models/issue.go Outdated
@@ -1209,7 +1209,10 @@ func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
userIDs := make([]int64, 0, 5)
if err := x.Table("comment").Cols("poster_id").
Where("issue_id = ?", issueID).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this really be?

Where("`comment`.issue_id = ?", issue).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since issue_id is a unique attribute in this join it is valid sql, but I can add it for the sake of clarity.

@@ -91,6 +91,9 @@ func GetIssueWatchers(issueID int64) ([]*IssueWatch, error) {
func getIssueWatchers(e Engine, issueID int64) (watches []*IssueWatch, err error) {
err = e.
Where("issue_id = ?", issueID).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, should have watch.issue_id

Copy link
Member Author

@daviian daviian Sep 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the other comment, I can add it for the sake of clarity, but it wouldn't be necessary.

@daviian daviian force-pushed the inactive-user-receive-mail-fix branch 2 times, most recently from c00221a to 4f36800 Compare September 12, 2017 08:14
Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor thing though

_, err = x.Exec("ALTER TABLE \"user\" ALTER COLUMN `prohibit_login` SET NOT NULL, ALTER COLUMN `prohibit_login` SET DEFAULT false")
case "mssql":
// xorm already set DEFAULT 0 for data type BIT in mssql
_, err = x.Exec("ALTER TABLE [user] ALTER COLUMN \"prohibit_login\" BIT NOT NULL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_, err = x.Exec(`ALTER TABLE [user] ALTER COLUMN "prohibit_login" BIT NOT NULL`)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I ask why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you can't :P I think bkcsoft asked that just for readability to not have to use backslashes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it should be changed in the other statements too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say no, because the other statements contain backticks

@daviian daviian force-pushed the inactive-user-receive-mail-fix branch 2 times, most recently from acc426a to ced11bd Compare September 13, 2017 21:42
@codecov-io
Copy link

codecov-io commented Sep 13, 2017

Codecov Report

Merging #2384 into master will decrease coverage by 0.11%.
The diff coverage is 73.68%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2384      +/-   ##
==========================================
- Coverage    27.7%   27.59%   -0.12%     
==========================================
  Files          83       83              
  Lines       16914    16928      +14     
==========================================
- Hits         4686     4671      -15     
- Misses      11553    11581      +28     
- Partials      675      676       +1
Impacted Files Coverage Δ
models/user.go 18.45% <ø> (ø) ⬆️
models/issue_mail.go 5.97% <0%> (-0.38%) ⬇️
models/issue_watch.go 84.21% <100%> (+0.87%) ⬆️
models/issue.go 24.64% <100%> (+0.22%) ⬆️
models/repo_watch.go 68.25% <100%> (+2.15%) ⬆️
models/notification.go 58.6% <0%> (-13.45%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b496e3e...7047786. Read the comment docs.

@lafriks
Copy link
Member

lafriks commented Sep 14, 2017

@bkcsoft need your approval

…ications

Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
@daviian daviian force-pushed the inactive-user-receive-mail-fix branch from f0092d9 to fdc0f6c Compare September 14, 2017 21:27
@daviian
Copy link
Member Author

daviian commented Sep 14, 2017

Rebased with current master

if err != nil {
return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
}
if issue.PosterID != doer.ID && poster.IsActive && !poster.ProhibitLogin {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing testing in this patch?.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@appleboy what do you mean?

@bkcsoft
Copy link
Member

bkcsoft commented Sep 15, 2017

LGTM

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Sep 15, 2017
@lunny lunny merged commit d766d0c into go-gitea:master Sep 16, 2017
@daviian daviian deleted the inactive-user-receive-mail-fix branch September 16, 2017 22:38
@lunny lunny mentioned this pull request Sep 22, 2017
7 tasks
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants