Code Monkey home page Code Monkey logo

Comments (6)

Dresdn avatar Dresdn commented on August 25, 2024

Without a stack trace, I'm going to guess you're seeing the error on line 134 here. Is that correct?

Are you removing the UserTenantPermissions beforehand? Are you setting them at all? I would also ask how you're adding the user to the tenant as the Tenant.add_user() creates the UserTenantPermissions, so they should be there when attempting to remove.

I suppose this could be refactored to be a little safer in case the UserTenantPermissions are removed ahead of time.

from django-tenant-users.

premudeshi avatar premudeshi commented on August 25, 2024

Yes the issue is line 134.
I am not removing nor setting them. I am adding them like:

userT = get_object_or_404(UserTenantPermissions, profile=user)
perm = Permission.objects.get(codename=request.POST.get('codename'))
userT.user_permissions.add(perm)

This snippet works. But for some reason removing it doesn't. I will try some variations to see how to remove it.

from django-tenant-users.

Dresdn avatar Dresdn commented on August 25, 2024

Since the removing fails on line 134, then there isn't a UserTenantPermissions object for the user within the tenant you're trying to remove the user from. Have you verified the object exists?

from django-tenant-users.

kusime avatar kusime commented on August 25, 2024

I am facing same problem now , but i fix this issue by practicing the document way to add new user ,and bind a user to a company.

  1. when you add new user you should use User.objects.create_user
    class UserRegisterSerializer(serializers.ModelSerializer):
    class Meta:
    model = User
    fields = ["id", "username", "email", "password"]
    read_only_fields = ["id"]

    def create(self, validated_data):
    # disable the account activation by default
    validated_data["is_active"] = False
    instance = User.objects.create_user(**validated_data)
    send_activate_email.send(
    sender="UserRegisterSerializer.create",
    request=self.context["request"],
    instance=instance,
    )
    return instance

by the way you also need to decalre the User manager with the objects = UserProfileManager() so that you can use that method

  1. when create new company(tenant) you should use provision_tenant to create ,this will do batch of stuff behind

  2. when bind new user to a company, you should use TenantBase.add_user . this method will link the company-user m2m field , and create a record in UserTenantPermissions in the correct schema. the table name is permissions_usertenantpermissions.
    @action(methods=[HTTPMethod.POST], detail=True)
    def add_user(self, request, pk=None):
    company = self.get_object()
    email = request.data.get("user_email", None)
    if email is None:
    return Response(
    status=status.HTTP_400_BAD_REQUEST, data={"detail": "user_email should not be null"}
    )
    user = User.objects.filter(email=email).first()
    company.add_user(user)
    return Response(status=status.HTTP_200_OK, data={"detail": "ok"})

for the transfer_ownership function ,you also make sure the company owner also have record in permissions_usertenantpermissions table ,

by the way , as i tried out , i find the django admin dashboard not calling the recommend method to create an new company or link the user to a company. even you override the Model.save() method .

from django-tenant-users.

Dresdn avatar Dresdn commented on August 25, 2024

@kusime - The formatting of your comment is a little off, seems you may have missed a backtick or something.

To your points:

  1. Use User.objects.create_user() - Yes, this is general good practice, but it's just a helper function that creates the User object and then adds the object to the Public Schema. Honestly, this whole function should be cleaned up a bit as it doesn't make sense to only require it to be run in the public schema, we can set that.
  2. Use provision_tenant() - 100%. There's extra params set when interfacing with django-tenants when creating the tenant.
  3. Use TenantBase.add_user() - 100%. I think this is what @premudeshi is missing. This adds a UserTenantPermissions object within the specific schema and is referenced when removing a user too. If this object doesn't exist, right now we're throwing an error.
  4. Admin Dashboard - @kusime, this is a great call out and could maybe be something we add to the documentation. Having a template that can be used as the ModelAdmin that uses all the appropriate calls would be a great thing to have.

from django-tenant-users.

premudeshi avatar premudeshi commented on August 25, 2024

It seems my Django User models messed up the migrations, I am guessing because of incorrectly configured inheritance.
Deleted migrations and started fresh, it worked.

from django-tenant-users.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.