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

Deny admin self deletion and self admin rights revocation #221

Merged
merged 7 commits into from
Dec 4, 2023

Conversation

stage-rl
Copy link
Collaborator

No description provided.

@stage-rl stage-rl requested a review from jsuchal November 30, 2023 17:36
def delete_user_group
def before_destroy
if self == Current.user
errors.add :name, "Administrátor nemôže zmazať svojho používateľa"
Copy link
Member

Choose a reason for hiding this comment

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

Tu chceme asi povedat, ze nemozes zmazat sam seba a idealne by bolo aby ten button ani nebolo vidiet.

</div>
<div class="grow shrink basis-0 flex-col justify-start items-start gap-1 inline-flex">
<div class="text-center text-gray-900 text-lg font-medium leading-loose"><%= @user.name %></div>
<div class="text-center text-gray-500 text-base font-normal leading-normal"><%= @user.email %></div>
</div>
<%= button_to admin_tenant_group_group_membership_path(Current.tenant, @group_membership.group, @group_membership), method: :delete do %>
<%= render Common::DeleteButtonComponent.new %>
<% unless @group_membership.group.type == "AdminGroup" && @group_membership.user == Current.user %>
Copy link
Member

Choose a reason for hiding this comment

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

nemame na toto presne tie policy objekty ci ideme duplikovat logiku?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Kedze tu chcem ten objekt sice vymenovat, len neumoznit mazanie, tak neviem ako tu pouzit policy. Takze ak, tak daj pls vediet ako, alebo posli priklad. Neukazat, ze tam ten admin je, mi pripada cudne

Copy link
Member

Choose a reason for hiding this comment

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

image

Toto je zdokumentacie pundita. Cize tu by som ja cakal nieco ako

if policy(@group_membership).delete? ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ano, uz som si to aj ja nasiel. A spravil. A hned som mal na pozadi warning, ze urcite sa ti nebude pacit toto v komponente (lebo select do DB). Napriek tomu, ze pundit vyslovene vyzyva k pouzivaniu vo viewoch

Copy link
Member

Choose a reason for hiding this comment

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

To, kde sa vykona ten select je vecou toho ako sa tie objekty hore loadnu. Cize nemusi to nutne znamenat ziadny select navyse.

@jsuchal
Copy link
Member

jsuchal commented Nov 30, 2023 via email

@stage-rl stage-rl requested a review from jsuchal December 3, 2023 14:21
def destroy?
@user.site_admin? || @user.admin?
(@user.site_admin? || @user.admin?) && !(@group_membership.user == @user && @group_membership.group.type == 'AdminGroup')
Copy link
Member

Choose a reason for hiding this comment

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

Toto by som rozdelil aspon na dva riadky lebo sa to zle cita a su to vlastne rozne podmienky

nieco v zmysle:

return false if @group_membership.user == @user
return true if @user.site_admin? || @user.admin?
return true

imho by sam seba si nemal vediet zmazat vobec nikdy, nejako sa mi tam nepozdava to obmedzenie na admin group

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ale tak to je odobranie sa zo skupiny (nie zmazanie usera). A admin moze chciet napr nieco otestovat, hodi sa do skupiny, a nasledne sa z nej odoberie. Aktualne mi len podpisovanie napada.

Copy link
Member

Choose a reason for hiding this comment

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

Ano myslel som tym clenstvo v skupine vseobecne. Ale pravda, ze undo by mal vediet asi spravit cize dava zmysel, aby zmazal aj sam seba pokial si neodpali admina.

@@ -5,6 +5,7 @@ class Admin::UserPolicy < ApplicationPolicy

def initialize(user_logged_in, user_to_authorize)
@user = user_logged_in
Copy link
Member

Choose a reason for hiding this comment

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

@actor ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nejako som to ponastavoval, ale pozri, ci si s tym takto OK. Vyrabam tam natvrdo metodu user, kedze Pundit celkom vyzera, ze s nou pocita (vsade je tam attr_reader :user). Aj ked aj bez nej mi to islo. Trochu sa bojim takychto zasahov mimo zdokumentovanych standardov

@@ -42,7 +43,6 @@ def edit?
end

def destroy?
@user.site_admin? || @user.admin?
(@user.site_admin? || @user.admin?) && @user_to_authorize != @user
Copy link
Member

Choose a reason for hiding this comment

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

Toto tiez rozdelme na riadky.

@user.site_admin? || @user.admin?
end
return false unless @actor.site_admin? || @actor.admin?
return true unless @user_to_authorize != @actor
Copy link
Member

Choose a reason for hiding this comment

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

mas tu dva zapory.

Suggested change
return true unless @user_to_authorize != @actor
return false if @user_to_authorize == @actor

private

def validate_self_admin_removal
return unless group.type == 'AdminGroup' && user == Current.user
Copy link
Member

Choose a reason for hiding this comment

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

Toto tu je este potrebne? Resp vidim tu este duplicitu

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Beriem teda, ze hovoris, ze princip je chytat taketo chyby co najvyssie, a neduplikovat to "dole". Tak?

Copy link
Member

Choose a reason for hiding this comment

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

Kedze pouzivame pundit, ktory vytrhava security checky do samostatneho layer, tak to drzme teda tam.

has_many :message_drafts, foreign_key: :author_id
has_many :automation_rules, class_name: 'Automation::Rule'
has_many :filters, foreign_key: :author_id

validates_presence_of :name, :email
validates_uniqueness_of :name, :email, scope: :tenant_id, case_sensitive: false

before_destroy :delete_user_group, prepend: true
before_destroy :before_destroy, prepend: true
Copy link
Member

Choose a reason for hiding this comment

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

Toto bolo pekne pomenovane, ze co to urobi a teraz uz nevies co to urobi. Netreba sa hanbit mat aj 2 metody zavesene na before_destroy hook ak to dava zmysel

@stage-rl stage-rl requested a review from jsuchal December 4, 2023 08:36
@stage-rl stage-rl merged commit 8592c14 into main Dec 4, 2023
@jsuchal jsuchal deleted the GO-401/deny_self_user_deletion branch December 24, 2023 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants