
    `h.                     4   S SK JrJrJrJr  S SKJrJrJr  S SK	J
r
  S SKJr  S SKJr  S SKJrJrJr  S SKJrJrJr  S SKJr  S S	KJrJr  \" 5       r\R;                  S
S/S9\" \5      4S j5       rS\S\\\\    4   4S jr!S\ S\\"   4S jr#S\S\\"   4S jr$\R;                  SS/S9\" \RJ                  5      \" \5      4S\RL                  4S jj5       r'\RQ                  SS/S9\" \RJ                  5      \" \5      4S\RL                  4S jj5       r)g)    )	APIRouterDependsHTTPExceptionstatus)OptionalDictList)jsonable_encoder)ObjectId)database)Role
RoleCreate
RoleUpdate)create_role_serviceupdate_role_serviceget_role_service)get_current_userdetails)features_listdefault_rolesz	/featuresFeatures)tagsc                 z   #    U R                  S5      nUS:X  a  [        $ US:X  a  S[        S   0$ [        SSS9e7f)z
Returns the list of features.
  - If current user has role 1 (Superadmin), return both SaaS and Platform features.
  - If current user has role 100 (Account admin), return only Platform features.
roles   d   platform  Insufficient permissionsstatus_codedetail)getr   r   )current_user	user_roles     FC:\Suresh\moveshuttle\MDcreated\moveengine\app\v1\routers\saas\rbac.pyget_featuresr&      sJ        )IA~	c	M*5664NOOs   9;role_idreturnc                    0 nU S::  Ga  [         S   nU S:X  a$  / SQnU H  nUR                  5       XS   '   M     U$ U S:X  a3  1 Skn/ SQnU H#  nUS   U;   d  M  UR                  5       XS   '   M%     U$ U S	:X  a3  1 S
kn/ SQnU H#  nUS   U;   d  M  UR                  5       XS   '   M%     U$ U S:X  a3  1 S
kn/ SQnU H#  nUS   U;   d  M  UR                  5       XS   '   M%     U$ U S:X  a0  S1n/ SQnU H#  nUS   U;   d  M  UR                  5       XS   '   M%     U$ U S:  Ga  [         S   nU S:X  a3  / SQnSS1nU H#  nUS   U;  d  M  UR                  5       XS   '   M%     U$ U S:X  a3  / SQnSS1nU H#  nUS   U;  d  M  UR                  5       XS   '   M%     U$ U S:X  a'  U H  nUS   S;   a  / SQXS   '   M  S/XS   '   M!     U$ U S:X  a#  SS1nU H  nUS   U;   d  M  / SQXS   '   M     U$ U S:X  a#  1 SknU H  nUS   U;   d  M  / SQXS   '   M     U$ U S:X  a  U H  nUS   S:X  d  M  S/XS   '   M     U$ [         R                  " 5        H  nU H  nS/XS   '   M     M     U$ )ua  
Generate a permissions mapping for a given system role based on its name, using module IDs as keys.

SaaS roles:
  - Superadmin: Full access (IDs 1-13, all actions).
  - Sales/Accounts: Access to modules [1,2,3,4,5,6,10,11,12] with actions: create, read, update.
  - Partner (and Tech Team): Limited access (only IDs [1,2,3,4,5,10,11,12]) with actions: create, read, update.

Platform roles:
  - Admin: Full access to all modules (IDs 21–35).
  - Manager: All modules with actions: create, read, update, delete.
  - Finance: For Transactions (ID 32) and Invoices (ID 33), full access; read-only for other modules.
  - Customer: Only access to modules [22, 23] with actions: create, read, update.
  - Vendor: Limited access to modules [21, 24, 25, 26, 27] with actions: create, read, update.
  - Workforce: Only access to the Workforce module (ID 27, read-only).
c   saasr   )createreadupdatedeleteid   >	   r   r1               
         )r,   r-   r.   r2   >   r   r1   r2   r3   r4   r6   r7   r8   r3   r4   r7   r   r         e   f   >       !   r-   h   i   >                  g   rE   )r   copyvalues)r'   permissionsmodulesactionsfeatureallowed_idsdisabled_idscategorys           r%   generate_role_permissionsrP      s$   " )+K |'a<<G"-4\\^DM* #L I \8K2G"4=K/18K. #B } \5K2G"4=K/18K. #v o \5K2G"4=K/18K. #h a \$K<G"4=K/18K. #Z Q 
C
+c><G8L"4=418K. #F A ^<G8L"4=418K. #: 5 ^"4=H,1WK.28K.	 #2 ) ^r(K"4=K/1MK. #$  ^.K"4=K/1MK. #  ^"4=B&28K. # 	 &,,.H#.4XDM* $ /     namec                 <   #    U S   nUR                  USS.5      $ 7f)z<
Check if a system default role with the given name exists.
r   T)rR   is_system_defaultfind_one)dbrR   roles_collections      r%   get_role_by_namerY   ~   s'      '{$$d%NOO   r0   c                 <   #    U S   nUR                  USS.5      $ 7f)z?
Check if a system default role with the given role_id exists.
r   T)r'   rT   rU   )rW   r0   rX   s      r%   get_role_by_idr\      s'      '{$$$%OPPrZ   z/accessRBACrW   c                 
  #    UR                  S5      nUS:X  a  [        nOUS:X  a  S[        S   0nO
[        SSS9eUR                  S5      nU(       d  S	U0$ [        X@5      I S
h  vN nU(       d
  [        SSS9eX5S.$  N7f)zf
Returns the list of features (tailored to the user's role) and the role rights from the roles table.
r   r   r   r   r   r   r   r'   rJ   Ni  zRole not found)rJ   role)r"   r   r   r   )rW   r#   r$   rJ   r'   r_   s         r%   
get_accessr`      s        )IA~	c	}Z894NOOy)G7##!'..D4DEE--	 /s   A%B'B(Bz/defaultRolesc                 `  #    UR                  S5      S:w  a
  [        SSS9eU S   n[        R                  " 5        HL  u  p4U HA  nSU;  a  US    S	3US'   UR	                  5       nX6S
'   UR                  US   US.SU0SS9  MC     MN     [        S [         5       5      nU S   nUR                  SS0SSU00SS9  / n	[         H  n
[        X
S   5      I Sh  vN n[        U
S   5      nU(       aR  U
S   U
S   UU
S   U
S   U
R                  SS5      S.n[        S 0 UD6n[        US   X5      nU	R                  U5        M  [        U
S   U
S   U
S   USU
S   U
S   U
R                  SS5      S9n[        UU 5      nU	R                  U5        M     [        SU	0[         ["        0S9$  N7f)!ag  
Create or update default system roles and insert the features list into the features collection.

This endpoint performs the following:
  1. Inserts/updates the features list (with descriptions) into a dedicated "features" collection.
  2. Updates the features counter (using _id: "features_id") so new features have an appropriate ID.
  3. Creates or updates default system roles with mapped permissions.

Default system roles:
  - SaaS Roles (role_id 1-4): 
      1: Superadmin (full access, is_global_access=True, is_saas_only=True)
      2: Sales/Accounts (access only to allowed modules, is_global_access=True, is_saas_only=True)
      3: Partner (limited access, is_global_access=False, is_saas_only=True)
      4: Tech Team (limited access, is_global_access=False, is_saas_only=True)
  - Platform Roles (role_id 100+):
      100: Admin (full access, is_global_access=True)
      101: Manager (read & update, is_global_access=True)
      102: Finance (full on Transactions/Invoices, read-only otherwise, is_global_access=True)
      103: Workforce (minimal; only Workforce module, is_global_access=False)
      104: Customer (access only to Dashboard and Booking, read-only, is_global_access=True)
      105: Vendor (limited access, is_global_access=False)

These roles are marked with is_system_default=True so they are not modifiable.
System roles do not use an account_id.
r   r   r   z.Not authorized to create default system roles.r   featuresdescriptionrR   z featurerO   r0   )features_idrO   z$setT)upsertc              3   L   #    U  H  n[         U     H	  o"S    v   M     M     g7f)r0   N)r   ).0rO   rL   s      r%   	<genexpr>.create_default_system_roles.<locals>.<genexpr>   s$      &3(=QYCZCZms   "$counters_idre   sequence_valuer'   NrT   is_global_accessis_saas_onlyF)rR   rd   rI   rT   rn   ro    )r'   rR   rd   rI   
account_idrT   rn   ro   created_roles)custom_encoder )r"   r   r   itemsrG   
update_onemaxr   r\   rP   r   r   appendr   r   r
   r   str)rW   r#   features_collectionrO   rc   rL   feature_docmax_feature_idcounters_collectionrr   role_defexistingrI   update_datarole_update_modelupdated_rolerole_createnew_roles                     r%   create_default_system_rolesr      s#    <  A%4dee Z.+113GG+,3FO+<H)E&!,,.K&.
#** +D 1xH% +    4  &3 N Z.""		"N34 #  M!'Y,?@@/0CD ('6*%-.A%B$,-?$@ (^U CK !+ 9[ 9.x	/BDUZL  .$ +f%$]3'"*+>"?!)*<!=%\\.%@	K +;;H  *9 "< _m<hX[_]]; As   CF.F,CF.N)*fastapir   r   r   r   typingr   r   r	   fastapi.encodersr
   bsonr   app.dbr   app.v1.models.saas.rolemodelr   r   r   app.v1.services.saas.rolesr   r   r   app.v1.dependencies.authr   app.v1.models.defaultr   r   routerr"   r&   intry   rP   dictrY   r\   get_mongo_dbMongoDBr`   postr   rt   rQ   r%   <module>r      sO   = = ' ' -   E E a a < >	 Kzl+&-.E&F P ,P^s ^tCcN/C ^@PS PXd^ PQ Q$ Q IVH%"8#8#8923.. &.2 Zwi("8#8#8923Z^Z^ )Z^rQ   