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

Feat add role base #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MartinAAcebeyL
Copy link

Pull Request description

Solve #15
We need to create and manage user roles within a mentorship system. Roles will be updated automatically when we add or remove data in links. This will be achieved by inserting roles into the auth_group table and creating PostgreSQL functions and triggers.

  • Insert Roles into auth_group
INSERT INTO auth_group(id, name) VALUES
(1, 'mentee'),
(2, 'mentor'),
(3, 'observer');
  • Create Function update_user_roles and Trigger
-- creacion de triger
create or replace function update_user_roles()
returns trigger as $$
declare
    group_mentee_id INT := 1;
    group_mentor_id INT := 2;
    mentor_exists boolean;
    mentee_exists boolean;
begin
    -- check if NEW.mentee_id exists in user_roles
    select exists(select 1 from users_user_groups where user_id = NEW.mentee_id and group_id = group_mentee_id) into mentee_exists;
    -- check if NEW.mentor_id exists in user_roles
    select exists(select 1 from users_user_groups where user_id = NEW.mentor_id and group_id = group_mentor_id) into mentor_exists;

    if not mentee_exists then
        insert into users_user_groups (user_id, group_id) values (NEW.mentee_id, group_mentee_id);
    end if;

    if not mentor_exists then
        insert into users_user_groups (user_id, group_id) values (NEW.mentor_id, group_mentor_id);
    end if;

    return NEW;
end;
$$ LANGUAGE plpgsql;


create trigger update_user_roles_trigger
AFTER INSERT ON one_on_one_link
FOR EACH ROW
EXECUTE FUNCTION update_user_roles();
  • Create Function update_user_roles_after_delete and Trigger
CREATE OR REPLACE FUNCTION update_user_roles_after_delete()
RETURNS trigger AS $$
DECLARE
    group_mentee_id INT := 1;
    group_mentor_id INT := 2;
    mentor_still_exists BOOLEAN;
    mentee_still_exists BOOLEAN;
BEGIN
    -- Check if the deleted mentee_id still exists in any mentorship
    SELECT EXISTS(SELECT 1 FROM one_on_one_link WHERE mentee_id = OLD.mentee_id) INTO mentee_still_exists;

    -- Check if the deleted mentor_id still exists in any mentorship
    SELECT EXISTS(SELECT 1 FROM one_on_one_link WHERE mentor_id = OLD.mentor_id) INTO mentor_still_exists;

    -- If the mentee_id no longer exists in any mentorship, remove them from the user group
    IF NOT mentee_still_exists THEN
        DELETE FROM users_user_groups WHERE user_id = OLD.mentee_id AND group_id = group_mentee_id;
    END IF;

    -- If the mentor_id no longer exists in any mentorship, remove them from the user group
    IF NOT mentor_still_exists THEN
        DELETE FROM users_user_groups WHERE user_id = OLD.mentor_id AND group_id = group_mentor_id;
    END IF;

    RETURN OLD;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER after_delete_mentorship
AFTER DELETE ON one_on_one_link
FOR EACH ROW
EXECUTE FUNCTION update_user_roles_after_delete();

How to test these changes

We need to launch the project, then we must add or delete Links, the changes will be automatically reflected in the users_user_groups table
Screenshot from 2024-09-02 22-44-55

Pull Request checklists

This PR is a:

  • [ x] bug-fix
  • [ x] new feature
  • maintenance

About this PR:

  • it includes tests.
  • the tests are executed on CI.
  • the tests generate log file(s) (path).
  • pre-commit hooks were executed locally.
  • this PR requires a project documentation update.

Author's checklist:

  • I have reviewed the changes and it contains no misspelling.
  • The code is well commented, especially in the parts that contain more
    complexity.
  • New and old tests passed locally.

Reviewer's checklist

Copy and paste this template for your review's note:

## Reviewer's Checklist

- [ ] I managed to reproduce the problem locally from the `main` branch
- [ ] I managed to test the new changes locally
- [ ] I confirm that the issues mentioned were fixed/resolved

@xmnlab
Copy link
Member

xmnlab commented Sep 9, 2024

@MartinAAcebeyL thanks for working on that .. just saw it now .. could you rebase your PR please?

btw, I also made some changes last week .. I pointed the default postgres port to 45432 .. to avoid any conflict to any local postgresql.

btw I am not seeing anything about roles here .. is it already ready for review?

@MartinAAcebeyL
Copy link
Author

Hi Ivan, sorry for the delay.

Great, I just performed the rebase.
What I did for the task is mainly to create triggers for the one_on_one_link table, so that records are automatically created or deleted in the users_user_groups table, this table created by django maintains a relationship between users and roles, roles that are stored in the auth_group table.

I think that with this we can already distinguish if the user is a mentor, mentee or an observer.

@xmnlab
Copy link
Member

xmnlab commented Sep 21, 2024

@MartinAAcebeyL thanks for working on that .. I will review this PR this weekend. thanks

@xmnlab
Copy link
Member

xmnlab commented Sep 28, 2024

@MartinAAcebeyL .. have you pushed your changes? I am just seeing very small changes that is not related to the title of this PR

@MartinAAcebeyL
Copy link
Author

Hi Ivan, sorry for the delay.
I think it's done, I think it solves the problem described in the point "What's the problem this feature will solve?"; due to the triggers we can now differentiate between who is a mentor and who is a tutee, this would be reflected in the users_user groups table.

What is missing, Ivan?

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