def authorized(data): next_url = request.args.get('state') or url_for('index') print 'oauth authorized', request.args if current_user.is_authenticated: return redirect(next_url) # store access token in the session access_token = data['access_token'] session['access_token'] = access_token, '' # get user's email from google headers = {'Authorization': 'OAuth ' + access_token} response = requests.get('https://www.googleapis.com/plus/v1/people/me', headers=headers) if response.status_code == 200: user_data = response.json() print 'user data', user_data email = [e for e in user_data['emails'] if e['type'] == 'ACCOUNT'][0]['value'] user = db_service.get_user_by_email(email) if user is None: # create user with random username username = random_username() while db_service.get_user_by_username(username) is not None: username = random_username() user = db_service.create_user(email, username, None, {}) login_user(user) else: print 'error getting google info', response.status_code, response.text return redirect(next_url)
def create_hypervisor(request): if request.method == constants.GET: return render(request, constants.CREATE_HYPERVISOR_TEMPLATE) hypervisor_type = request.POST['hypervisor_type'] protocol = request.POST['protocol'] host = request.POST['host'] port = request.POST['port'] if hypervisor_type == "openstack": domain = request.POST['domain'] username = request.POST['username'] password = request.POST['password'] if hypervisor_type == "openstack": adapter = factory.get_adapter( hypervisor_type, { constants.PROTOCOL: protocol, constants.HOST: host, constants.PORT: port, constants.DOMAIN: domain, 'username': username, 'password': password }) user_detail = adapter.create_sol_user() if constants.ERROR_MESSAGE in user_detail: return hypervisor_management( request, error_message=user_detail[constants.ERROR_MESSAGE]) hypervisor = db_service.create_hypervisor(hypervisor_type, protocol, host, port) user = db_service.get_user(constants.HYPERVISOR_SOLUSER_NAME) if not user: user = db_service.create_user({ constants.USERNAME: constants.HYPERVISOR_SOLUSER_NAME, constants.USER_EMAIL: constants.HYPERVISOR_SOLUSER_EMAIL, constants.USER_FULL_NAME: constants.HYPERVISOR_SOLUSER_NAME }) db_service.save_user_credentials(user.username, hypervisor.host, domain, constants.HYPERVISOR_SOLUSER_NAME, user_detail['user_password']) db_service.update_hypervisor_user_id(user.username, hypervisor.host, user_detail['user_id']) elif hypervisor_type == "vCenter": hypervisor = db_service.create_hypervisor(hypervisor_type, protocol, host, port) return hypervisor_management(request, message='Hypervisor added successfully.')
def create_user(request): """ create user method is used to create a user in AD and in sol db. GET request will load the template and POST request will create a user. :param request: :return: """ # in case if it's GET request redirect to create user page. if request.method == constants.GET: return render(request, constants.CREATE_USER_TEMPLATE) try: # get user details from auth AD and pass username user_detail = ad.retrieve_user_details(db_service.get_auth_ad(), request.POST["id"]) user_detail[constants.USER_PASSWORD] = request.POST["new_password"] # pass the user's details taken from auth AD to create user in local AD ad.create_user(db_service.get_local_ad(), user_detail) except Exception as e: return list_sol_users(request, error_message=str(e)) # add that created user in SOL database db_service.create_user(user_detail) subject = " User(created): Welcome to ServiceOnline." users_information_table = tabulate( [["Name : ", user_detail[constants.USER_FULL_NAME]], ["EmailAddress : ", user_detail[constants.USER_EMAIL]], ["Username : "******"Password:"******"Hi " + fname + ", \n\tWelcome to ServiceOnline. \nRecently Administrator created your account on " \ "ServiceOnline. Please find the access details below, \n\n" + users_information_table + \ "\n\nIn case of any access related issue, please get in touch with Administrator." sol_email.send_mail(receiver=user_detail[constants.USER_EMAIL], subject=subject, message=message) return list_sol_users(request, message="User created successfully.")