<s> import time <EOL> from flask import request <EOL> from flask_restful import abort <EOL> from funcy import project <EOL> from peewee import IntegrityError <EOL> from redash import models <EOL> from redash . permissions import require_permission , require_admin_or_owner , is_admin_or_owner , require_permission_or_owner , require_admin <EOL> from redash . handlers . base import BaseResource , require_fields , get_object_or_404 <EOL> from redash . authentication . account import invite_link_for_user , send_invite_email , send_password_reset_email <EOL> def invite_user ( org , inviter , user ) : <EOL> invite_url = invite_link_for_user ( user ) <EOL> send_invite_email ( inviter , user , invite_url , org ) <EOL> return invite_url <EOL> class UserListResource ( BaseResource ) : <EOL> @ require_permission ( '<STR_LIT>' ) <EOL> def get ( self ) : <EOL> return [ u . to_dict ( ) for u in models . User . all ( self . current_org ) ] <EOL> @ require_admin <EOL> def post ( self ) : <EOL> req = request . get_json ( force = True ) <EOL> require_fields ( req , ( '<STR_LIT:name>' , '<STR_LIT:email>' ) ) <EOL> user = models . User ( org = self . current_org , <EOL> name = req [ '<STR_LIT:name>' ] , <EOL> email = req [ '<STR_LIT:email>' ] , <EOL> groups = [ self . current_org . default_group . id ] ) <EOL> try : <EOL> user . save ( ) <EOL> except IntegrityError as e : <EOL> if "<STR_LIT:email>" in e . message : <EOL> abort ( <NUM_LIT> , message = '<STR_LIT>' ) <EOL> abort ( <NUM_LIT> ) <EOL> self . record_event ( { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : int ( time . time ( ) ) , <EOL> '<STR_LIT>' : user . id , <EOL> '<STR_LIT>' : '<STR_LIT:user>' <EOL> } ) <EOL> invite_url = invite_user ( self . current_org , self . current_user , user ) <EOL> d = user . to_dict ( ) <EOL> d [ '<STR_LIT>' ] = invite_url <EOL> return d <EOL> class UserInviteResource ( BaseResource ) : <EOL> @ require_admin <EOL> def post ( self , user_id ) : <EOL> user = models . User . get_by_id_and_org ( user_id , self . current_org ) <EOL> invite_url = invite_user ( self . current_org , self . current_user , user ) <EOL> d = user . to_dict ( ) <EOL> d [ '<STR_LIT>' ] = invite_url <EOL> return d <EOL> class UserResetPasswordResource ( BaseResource ) : <EOL> @ require_admin <EOL> def post ( self , user_id ) : <EOL> user = models . User . get_by_id_and_org ( user_id , self . current_org ) <EOL> reset_link = send_password_reset_email ( user ) <EOL> class UserResource ( BaseResource ) : <EOL> def get ( self , user_id ) : <EOL> require_permission_or_owner ( '<STR_LIT>' , user_id ) <EOL> user = get_object_or_404 ( models . User . get_by_id_and_org , user_id , self . current_org ) <EOL> return user . to_dict ( with_api_key = is_admin_or_owner ( user_id ) ) <EOL> def post ( self , user_id ) : <EOL> require_admin_or_owner ( user_id ) <EOL> user = models . User . get_by_id_and_org ( user_id , self . current_org ) <EOL> req = request . get_json ( True ) <EOL> params = project ( req , ( '<STR_LIT:email>' , '<STR_LIT:name>' , '<STR_LIT:password>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if '<STR_LIT:password>' in params and '<STR_LIT>' not in params : <EOL> abort ( <NUM_LIT> , message = "<STR_LIT>" ) <EOL> if '<STR_LIT>' in params and not user . verify_password ( params [ '<STR_LIT>' ] ) : <EOL> abort ( <NUM_LIT> , message = "<STR_LIT>" ) <EOL> if '<STR_LIT:password>' in params : <EOL> user . hash_password ( params . pop ( '<STR_LIT:password>' ) ) <EOL> params . pop ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in params and not self . current_user . has_permission ( '<STR_LIT>' ) : <EOL> abort ( <NUM_LIT> , message = "<STR_LIT>" ) <EOL> try : <EOL> user . update_instance ( ** params ) <EOL> except IntegrityError as e : <EOL> if "<STR_LIT:email>" in e . message : <EOL> message = "<STR_LIT>" <EOL> else : <EOL> message = "<STR_LIT>" <EOL> abort ( <NUM_LIT> , message = message ) <EOL> self . record_event ( { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : int ( time . time ( ) ) , <EOL> '<STR_LIT>' : user . id , <EOL> '<STR_LIT>' : '<STR_LIT:user>' , <EOL> '<STR_LIT>' : params . keys ( ) <EOL> } ) <EOL> return user . to_dict ( with_api_key = is_admin_or_owner ( user_id ) ) </s>
