<s> from __future__ import absolute_import , print_function , unicode_literals <EOL> import sys <EOL> import logging <EOL> from optparse import make_option <EOL> from django . core . management import call_command <EOL> from django . core . management . base import BaseCommand <EOL> from django . core . mail import mail_admins <EOL> from corehq import privileges <EOL> from django_prbac . models import Grant , Role <EOL> logger = logging . getLogger ( __name__ ) <EOL> BULK_CASE_AND_USER_MANAGEMENT = '<STR_LIT>' <EOL> CROSS_PROJECT_REPORTS = '<STR_LIT>' <EOL> def cchq_prbac_bootstrap ( apps , schema_editor ) : <EOL> """<STR_LIT>""" <EOL> call_command ( '<STR_LIT>' ) <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle ( self , dry_run = False , verbose = False , fresh_start = False , testing = False , * args , ** options ) : <EOL> self . verbose = verbose <EOL> if fresh_start : <EOL> confirm_fresh_start = raw_input ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if confirm_fresh_start == '<STR_LIT:yes>' : <EOL> self . flush_roles ( ) <EOL> for role in self . BOOTSTRAP_PRIVILEGES + self . BOOTSTRAP_PLANS : <EOL> self . ensure_role ( role , dry_run = dry_run ) <EOL> for ( plan_role_slug , privs ) in self . BOOTSTRAP_GRANTS . items ( ) : <EOL> for priv_role_slug in privs : <EOL> self . ensure_grant ( plan_role_slug , priv_role_slug , dry_run = dry_run ) <EOL> for old_priv in self . OLD_PRIVILEGES : <EOL> self . remove_grant ( old_priv ) <EOL> Role . objects . filter ( slug = old_priv ) . delete ( ) <EOL> def flush_roles ( self ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> Role . objects . all ( ) . delete ( ) <EOL> def ensure_role ( self , role , dry_run = False ) : <EOL> """<STR_LIT>""" <EOL> existing_roles = Role . objects . filter ( slug = role . slug ) <EOL> if existing_roles : <EOL> logger . info ( '<STR_LIT>' , role . name ) <EOL> return existing_roles [ <NUM_LIT:0> ] <EOL> else : <EOL> if dry_run : <EOL> logger . info ( '<STR_LIT>' , role . name ) <EOL> else : <EOL> if self . verbose : <EOL> logger . info ( '<STR_LIT>' , role . name ) <EOL> role . save ( ) <EOL> def ensure_grant ( self , grantee_slug , priv_slug , dry_run = False ) : <EOL> """<STR_LIT>""" <EOL> if dry_run : <EOL> grants = Grant . objects . filter ( from_role__slug = grantee_slug , <EOL> to_role__slug = priv_slug ) <EOL> if not grants : <EOL> logger . info ( '<STR_LIT>' , grantee_slug , priv_slug ) <EOL> else : <EOL> grantee = Role . objects . get ( slug = grantee_slug ) <EOL> priv = Role . objects . get ( slug = priv_slug ) <EOL> Role . get_cache ( ) . clear ( ) <EOL> if grantee . has_privilege ( priv ) : <EOL> if self . verbose : <EOL> logger . info ( '<STR_LIT>' , grantee . slug , priv . slug ) <EOL> else : <EOL> if self . verbose : <EOL> logger . info ( '<STR_LIT>' , grantee . slug , priv . slug ) <EOL> Grant . objects . create ( <EOL> from_role = grantee , <EOL> to_role = priv , <EOL> ) <EOL> def remove_grant ( self , priv_slug , dry_run = False ) : <EOL> grants = Grant . objects . filter ( to_role__slug = priv_slug ) <EOL> if dry_run : <EOL> if grants : <EOL> logger . info ( "<STR_LIT>" , priv_slug ) <EOL> else : <EOL> if grants : <EOL> grants . delete ( ) <EOL> logger . info ( "<STR_LIT>" , priv_slug ) <EOL> BOOTSTRAP_PRIVILEGES = [ <EOL> Role ( slug = privileges . API_ACCESS , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . LOOKUP_TABLES , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . CLOUDCARE , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . CUSTOM_BRANDING , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . ACTIVE_DATA_MANAGEMENT , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . CUSTOM_REPORTS , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . ROLE_BASED_ACCESS , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . OUTBOUND_SMS , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> ) , <EOL> Role ( slug = privileges . REMINDERS_FRAMEWORK , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> ) , <EOL> Role ( slug = privileges . CUSTOM_SMS_GATEWAY , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> ) , <EOL> Role ( slug = privileges . INBOUND_SMS , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . BULK_CASE_MANAGEMENT , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . BULK_USER_MANAGEMENT , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . DEIDENTIFIED_DATA , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . HIPAA_COMPLIANCE_ASSURANCE , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . ALLOW_EXCESS_USERS , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . COMMCARE_LOGO_UPLOADER , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . LOCATIONS , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . REPORT_BUILDER , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . USER_CASE , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . DATA_CLEANUP , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . TEMPLATED_INTENTS , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . CUSTOM_INTENTS , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) , <EOL> Role ( slug = privileges . ADVANCED_DOMAIN_SECURITY , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <EOL> ] <EOL> BOOTSTRAP_PLANS = [ <EOL> Role ( slug = '<STR_LIT>' , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = '<STR_LIT>' , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = '<STR_LIT>' , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = '<STR_LIT>' , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> Role ( slug = '<STR_LIT>' , name = '<STR_LIT>' , description = '<STR_LIT>' ) , <EOL> ] <EOL> community_plan_features = [ <EOL> ] <EOL> standard_plan_features = community_plan_features + [ <EOL> privileges . API_ACCESS , <EOL> privileges . LOOKUP_TABLES , <EOL> privileges . OUTBOUND_SMS , <EOL> privileges . REMINDERS_FRAMEWORK , <EOL> privileges . CUSTOM_SMS_GATEWAY , <EOL> privileges . ROLE_BASED_ACCESS , <EOL> privileges . BULK_USER_MANAGEMENT , <EOL> privileges . BULK_CASE_MANAGEMENT , <EOL> privileges . ALLOW_EXCESS_USERS , <EOL> privileges . LOCATIONS , <EOL> privileges . USER_CASE , <EOL> ] <EOL> pro_plan_features = standard_plan_features + [ <EOL> privileges . CLOUDCARE , <EOL> privileges . CUSTOM_REPORTS , <EOL> privileges . INBOUND_SMS , <EOL> privileges . HIPAA_COMPLIANCE_ASSURANCE , <EOL> privileges . DEIDENTIFIED_DATA , <EOL> privileges . REPORT_BUILDER , <EOL> privileges . DATA_CLEANUP , <EOL> privileges . TEMPLATED_INTENTS , <EOL> ] <EOL> advanced_plan_features = pro_plan_features + [ <EOL> privileges . CUSTOM_BRANDING , <EOL> privileges . ACTIVE_DATA_MANAGEMENT , <EOL> privileges . COMMCARE_LOGO_UPLOADER , <EOL> privileges . CUSTOM_INTENTS , <EOL> privileges . ADVANCED_DOMAIN_SECURITY <EOL> ] <EOL> enterprise_plan_features = advanced_plan_features + [ ] <EOL> OLD_PRIVILEGES = [ <EOL> BULK_CASE_AND_USER_MANAGEMENT , <EOL> CROSS_PROJECT_REPORTS , <EOL> ] <EOL> BOOTSTRAP_GRANTS = { <EOL> '<STR_LIT>' : community_plan_features , <EOL> '<STR_LIT>' : standard_plan_features , <EOL> '<STR_LIT>' : pro_plan_features , <EOL> '<STR_LIT>' : advanced_plan_features , <EOL> '<STR_LIT>' : enterprise_plan_features , <EOL> } </s>
