Managing Permissions With EF Core Migrations | Permission Authorization - Part 2

Поділитися
Вставка
  • Опубліковано 3 лют 2025

КОМЕНТАРІ • 99

  • @MilanJovanovicTech
    @MilanJovanovicTech  2 роки тому +3

    Get the source code for this video for FREE → the-dotnet-weekly.ck.page/permissions2
    Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
    Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

    • @TaureanKing83
      @TaureanKing83 2 роки тому

      Great content. Keep them coming. It would also be great to see a video for resource-based authorization.

  • @nouchance
    @nouchance 2 роки тому +3

    Thank you Milan✊💯🤝 From Uzbekistan!

  • @subashbarik
    @subashbarik 2 роки тому +2

    Awesome , waiting for the next video of this series.

  • @dunderball716
    @dunderball716 2 роки тому +1

    Awesome series, bro! Keep it up! Greetings from Bulgaria 🇧🇬!

  • @Mazur_Family_Travel
    @Mazur_Family_Travel 4 місяці тому +1

    Awesome! Liked, subscribed)
    Keep continuing to create such intensive and helpful lessons)

  • @laolaoal4784
    @laolaoal4784 Рік тому +1

    what a clean way to implement that!
    pretty interesting. thank u for this tutorial

  • @Corsair4Fun
    @Corsair4Fun 2 роки тому +1

    Keep up the good work Milan.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      Thank you!

    • @Corsair4Fun
      @Corsair4Fun 2 роки тому

      @@MilanJovanovicTech I also wanted to recommend a similiar library, but YT keeps removing my comments for some reason.

  • @fernandocalmet
    @fernandocalmet 2 роки тому +1

    As always your content is the best

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      Thanks, Fernando, I think you're one of the best supporters I have 😊

  • @ramytawfik9168
    @ramytawfik9168 Рік тому +1

    Great as always Milan 👌

  • @benij
    @benij 3 місяці тому +1

    Amazing way to implement that Milan! From Perú 💯!

  • @MaxSupercars
    @MaxSupercars 2 роки тому +1

    Great ideas! Thank you! Looking forward for continuation! 😊

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      Thank you, glad you liked it. The next video will put everything together 😁

    • @MaxSupercars
      @MaxSupercars 2 роки тому +1

      @@MilanJovanovicTech Great thank you! I like that you split this serie in more videos, it's easier to watch. It only got maybe disadvantage for new subscribers, that it's not all in 1 video. But it would be long and maybe less interesting to watch.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      @@MaxSupercars The entire authentication series is close to 90 minutes, so definitely need to split it

  • @jwcodendaal
    @jwcodendaal Рік тому +1

    Beautiful implementation. Top job.

  • @nove1398
    @nove1398 2 роки тому +1

    Another great video!

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      Thank you. Next one is the most important in the series 😁

  • @bykaos2012
    @bykaos2012 2 роки тому +1

    Thank you Milan for video

  • @danielle8624
    @danielle8624 Рік тому +5

    What's inside the Enumeration class? (in Gatherly.Domain.Primitives) Is there any video that covers this?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому +1

      Take a look at this video: ua-cam.com/video/v6cYTcEfZ8A/v-deo.html

  • @E_G_
    @E_G_ 2 роки тому +1

    Great Video. Thank you :)

  • @mylesdavies9476
    @mylesdavies9476 2 роки тому +1

    Really like the conversion of the enum to a new entity with key:value and the automatic seeding. I have always avoided use of enums with efcore for things like status values as I couldn't figure out a way to get the full key and value into the DB reliably. Will try to replicate this, thanks

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      You can also use value converters, and just store the enum value

    • @mylesdavies9476
      @mylesdavies9476 2 роки тому +1

      @@MilanJovanovicTech I've always wanted both in the DB though as it's useful to be able to see what the value means

  • @olufeming
    @olufeming Рік тому +2

    Hi Millan, I love the you structure your code and its so clean indeed, but then the more I watch the more confused I get. Is there anyway you can help by making things perhaps understandable or probably make recommendations on how to catchup.

  • @javidibra6786
    @javidibra6786 8 місяців тому

    Hi , First of all i wanna say that it is amazing content thanx for sharing such a great content. But there is a question that makes me think: after making our project working in production server , how we can add new permission enum? we only have to add that on code side? and after adding new permission enum we should make new migrate, how we can handle that situation with production code?

    • @MilanJovanovicTech
      @MilanJovanovicTech  8 місяців тому +1

      Create a new migration, and add the permissions to the DB

  • @Tamer_Ali
    @Tamer_Ali 4 місяці тому

    Is it better to use HasPermission attribute with IRequest query or command implementation as Jason Taylor did with Authorize attribute in clean architecture?

  • @VakhushtiMetreveli
    @VakhushtiMetreveli 2 роки тому +1

    I have sum issuse with this lection, The collection navigation 'Role' cannot be added to the entity type 'User' because its CLR type 'Role' does not implement 'IEnumerable'. Collection navigations must implement IEnumerable of the related entity.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      You probably did something different with the relationship configuration?

    • @VakhushtiMetreveli
      @VakhushtiMetreveli 2 роки тому

      @@MilanJovanovicTech i think no, in user config i have builder.HasOne(e => e.Role)
      .WithMany()
      .HasForeignKey("RoleID");

  • @Tamer_Ali
    @Tamer_Ali Рік тому +1

    Hi Milan,
    I'd like to categorize the Permissions and I'll create enum for them
    public enum PermissionCategory { Admins= 1, Users= 2}
    should I loop the Permission enum and add PermissionCategory value as the foreign key
    or create a custom attribute "say Category" with each Permission then use your way to seed permissions data?
    public enum Permission
    {
    [Category(PermissionCategory.Admins)]
    ManageRoles = 1,
    [Category(PermissionCategory.Users)]
    ManageProfiles = 2
    }

  • @ayedabboushi7221
    @ayedabboushi7221 Рік тому +2

    Enumeration is missing please reorder videos on the series

  • @paulbarton2280
    @paulbarton2280 Рік тому +2

    How do you prevent EF from trying to create a new Role (with a duplicate key) every time you save changes?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому +1

      Answered on Patreon also, but for anyone reading - just call Set.Attach(roleInstance) and it won't be an issue

    • @thesm9734
      @thesm9734 9 місяців тому

      @@MilanJovanovicTech That doesn't work when there are more Roles of the same type attached to more users while saving changes. Any clean idea on how to preload those Roles from the database ?
      For reference the error is: "The instance of entity type 'Role' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked".
      The error happens before I can even attach them (my approach was to use a save changes interceptor). It happens once the ChangeTracker gets read, as it tries to detect changes. Oh and also, I would rather not deactivate the auto detection of changes as it helps with saving changes.
      Thanks in advance for the answer!

    • @thesm9734
      @thesm9734 9 місяців тому

      Alternatively, I managed to create a backing field where I store only the Id for the Role inside the User class. Outside my domain I get access to the role by using a get only property that searches inside the Role.GetValues() for that Id, so when creating users, instead of letting ef core decide whether the Role is a new entity or not, I only persist the backing field Id and ignore the property altogether inside my db config. (I would still be interested in preloading data, as it may cover another use case that this example didn't)

  • @StressedProgrammer
    @StressedProgrammer 5 місяців тому

    whats the Enumeration class? is it some type of custom made enums?

  • @bilalmehrban
    @bilalmehrban 2 роки тому +1

    I have implemented this in two projects recently, one thing that I struggled with was how to make a menu out of this permission enum and only show those menu items which the user has permission to.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      You need to be able to read the permissions client side
      Either by fetching from DB, or passing them in JWT

  • @ShaxbozbekQodiov
    @ShaxbozbekQodiov Рік тому

    in which video you had writen Enumeration ?

  • @unimatrix20a
    @unimatrix20a Рік тому

    i watched your video on Enumerations but I dont find a GetValues static method there. Am I missing something?

  • @CaioVitorOliveira
    @CaioVitorOliveira 3 місяці тому

    I'm trying to test this aprouch and I have to initialize my Roles, Permissions, Users, RolePermissions and RoleUsers. My test database starts empty, how can I use linq to initialize those entities?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      Add them to the DB at startup

    • @CaioVitorOliveira
      @CaioVitorOliveira 3 місяці тому +1

      @@MilanJovanovicTech I'm sorry, I asked the question wrong. By following your videos I was able to implement the HasPermission method and initialize the permissions and related entities using IEntityTypeConfiguration and builder.HasData.
      The problem is in the API tests using xUnit, I use the CustomWebApplicationFactory class that inherits from WebApplicationFactory to initialize my databases but I'm not able to insert the related entities. Could you explain how to seed the Permissions, Roles and RolePermissions in the CustomWebApplicationFactory?

  • @RoshdiAlMajzoub
    @RoshdiAlMajzoub Рік тому

    I need your Help
    I am getting this error while running the application
    Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Authorization.IAuthorizeData Lifetime: Scoped ImplementationType: Gatherly.Infrastructure.Authentication.HasPermissionAttribute': Unable to resolve service for type 'Gatherly.Domain.Enums.Permission' while attempting to activate 'Gatherly.Infrastructure.Authentication.HasPermissionAttribute'.)

  • @velkumars26
    @velkumars26 Рік тому +1

    Hi Milan,
    What is the implementation for GetValue()

  • @_NguyenHaoTien
    @_NguyenHaoTien Рік тому

    Oh the video is so great,but i have a stuck when apply [authorize(Policy = "A")] for controller but i wanna authorize from Controller except some Action in Controller
    Ex: [authorize(Policy = "A", ExceptAction = "CreateTask")]
    I cannot write new attribute to override, can you help me the solution(.NET 6 or 8)?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому

      AllowAnonymous?

    • @_NguyenHaoTien
      @_NguyenHaoTien Рік тому

      @@MilanJovanovicTech no, i wanna put authorize on action method and ignore authorize on controller

  • @K1llRay64
    @K1llRay64 2 місяці тому

    Why would you use Permission enum as it is, when you introduced to smart enums? Isn't that creating too much chaos when you use both enum and smartenums in your project? and why you need smartenums If you just used Permission enum without hesitation?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 місяці тому

      Smart enums have behavior, but also let you simplify seeding

    • @K1llRay64
      @K1llRay64 2 місяці тому

      @@MilanJovanovicTech thanks for a reply!
      In that case, in a context of DDD, can I use smart Enumeration as a base class for my ValueObject also, in scenarios when I need static properties in my VO?

  • @abidalisidd
    @abidalisidd 2 роки тому

    Hi, I am your new viewer. I want that video link where you created this whole project structure. Please share.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      There isn't one starting from scratch

    • @abidalisidd
      @abidalisidd 2 роки тому

      @@MilanJovanovicTech I really like your project structure. Don't you think there should be a video to create a project structure on which you are currently working in this video so that your viewer can write and practice the concept on the same project structure.

  • @bykaos2012
    @bykaos2012 2 роки тому +1

    Hello Milan, I get an error when I want to Subscribe from Patreon. I just live on patreon. Anyone else having problems?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      That is very strange. Is it still not functioning? I didn't get any complaints recently.

    • @Tengri9090
      @Tengri9090 2 роки тому

      @@MilanJovanovicTech Yes that is right. I wanted to subscribe too, I got an error. Turkey does not accept credit cards.

  • @thiagomdemelo145
    @thiagomdemelo145 Рік тому +1

    If u are using classes that are not default or importable, u need to show us how they work or let we have the code...

  • @adewunmivincent4198
    @adewunmivincent4198 6 місяців тому

    How can I get source code

    • @MilanJovanovicTech
      @MilanJovanovicTech  6 місяців тому

      Start from here: ua-cam.com/video/4cFhYUK8wnc/v-deo.html

  • @prateektomar4874
    @prateektomar4874 9 місяців тому

    please provide the code link.

  • @pioners1001
    @pioners1001 2 роки тому

    What is the point of storing all the configuration in the database if it does not change (everything is seeded into DB)? If there would be a UI that would allow admins to change permissions for roles, then I would understand why. It would be better to keep it in memory - static classes with configuration would not take a lot of memory and would be much faster.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      How difficult would it be to expose a few endpoints for managing it? Not much with all of this in place.
      Also, you get the benefit of having FK constraints between Users and roles/permissions.

    • @pioners1001
      @pioners1001 2 роки тому +1

      @@MilanJovanovicTech That's true, but if you know that permissions and role-permission mapping will never change (or not change in near future), then implementing it with DB store is overkill.
      I have implemented permissions just like in your example but without DB and the system is in production for 3 years and we never had a need to change the mapping. We only add new features and new permissions. Configuration in code works well in such scenario.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      @@pioners1001 Except changing permissions in your case require downtime. While permissions in the DB can be added/removed without downtime.