mardi 25 août 2015

How to get data from a database by user's role?

I'm using Hibernate ORM (with MySQL) and JavaServer Faces (PrimeFaces). Here is project structure:

              .//////////////////-                      
           `++:`                 -+o-                   
           d`                       h`                  
           do.                    `+d                   
           d`:+///-..`    ``.-://+/`d                   
           d    ``.-::////::-.``   `h                   
           h        MySQL DB       `h                   
           y.     .===========     `h                   
           y.`:///:..```````.:/+/:``y                   
           ys+.`                `./ys                   
           y:   `....`             :y                   
           :s`  s:--++       /y-  `o/                   
            .//:h`  :o      /+`s//+.                    
              `.h`  :y::::/s+  `s:                      
                y`  :o````++    `s-                     
              /os`  -os: `oo+   y+o                     
              `s-    :s`   -s   d                       
          s////+y.  -y+////os   d/////s`                
          d     .y.-s`     -s   d     y.                
          d      .ss`      .s///y     y.                
          d       ``        ````      y.                
          d           DAOs            y.                
          d         =======           y.                
          d     .s//+o       ++       y.                
          d-----/s  .h------o//o------h.                
          ------/s  .h-----s+  /y------                 
                -s  .y    +s`  `s+                      
              `so+  `ss:  -/o  o/-                      
          `+///so    -h////+s  s+///////-               
          `h````/+  -s`````-s  s-``````:o               
          `y     /+-y`     .s::y.      :o               
          `y      +s`       ....       :o               
          `y        Services           :o               
          `y       =========           :o               
          `y    .---:         -        :o               
          `y    o/..d       `os+       :o               
          `h    o:  h`     `o: /o`     :o               
          `s////y:  h/////+h-   :h/////+:               
              `oy-  y+/   +oo   ss/                     
               o:   `s-     d   d                       
      `+///////+d: `ys//////d   m/////////////          
      .y         s:s:       h```h            h          
      .y          o:        :::::            h          
      .y                                     h          
      .y                                     h          
      .y               Beans                 h          
      .y              =======                h          
      .y                                     h          
      .y                                     h          
      .h/////////////////////////////////////h          
       ```````````````````````````````````````          

(I'm a novice here and haven't enought rights to post imeges :D)
I have a method "getAllDevices()" that returns all devices from DB. Users-Devices relations are many-to-many. An User has an role either "ADMIN" or "USER". I want to get devices relatively user's role: if "ADMIN" - all devices in the DB, else only user's devices. How do I make it clear and avoid swelling code in beans or services? May be I should use some pattern here?

Aucun commentaire:

Enregistrer un commentaire