Exemplo n.º 1
0
        public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            ExternalLoginResultModel externalLoginResultModel = new ExternalLoginResultModel();
            try
            {
                model.Country = string.IsNullOrWhiteSpace(model.Country) ? string.Empty : HttpUtility.UrlDecode(model.Country);
                ContactModel userModel = new ContactModel();
                OrganisationModel orgModel = new OrganisationModel();

                if (!this.ModelState.IsValid)
                {
                    return this.BadRequest(this.ModelState);
                }

                var verifiedAccessToken = await this.VerifyExternalAccessToken(model.Provider, model.ExternalAccessToken);
                if (verifiedAccessToken == null)
                {
                    return this.BadRequest("Invalid Provider or External Access Token");
                }

                var user = await this.authRepository.FindAsync(new UserLoginInfo(model.Provider, verifiedAccessToken.UserId));

                bool hasRegistered = user != null;

                if (!hasRegistered)
                {
                    userModel = this.GetExternalUserDetails(model.Provider, model.ExternalAccessToken);
                    if (string.IsNullOrEmpty(userModel.Email))
                    {
                        userModel.Email = model.Email;
                    }

                    userModel.GCMId = model.GCMId;
                    userModel.UDId = model.UDId;
                    userModel.OSType = model.OSType;
                    userModel.MailingCountry = model.Country;
                    userModel.MailingState = model.State;
                    userModel.CountryCode = model.CountryCode;
                    userModel.Timezone = model.Timezone;
                    userModel.Latitude = model.Latitude;
                    userModel.Longitude = model.Longitude;
                    userModel.IsAvailable = true;
                    userModel.Availability = Availability.AllDays;
                    userModel.AvailableFrom = "08:00:00";
                    userModel.AvailableTo = "20:00:00";
                    userModel.IsOnline = true;
                    userModel.IsActive = true;
                    string userName = userModel.Email;
                    if (string.IsNullOrWhiteSpace(userModel.Email))
                    {
                        userName = Guid.NewGuid().ToString();
                    }

                    user = new ApplicationUserDto
                    {
                        UserName = userName,
                        Email = userModel.Email,
                        Name = userModel.FirstName,
                        AccountType = (short)model.UserRole
                    };

                    if (model.Provider == "Facebook")
                    {
                        user.FacebookId = verifiedAccessToken.UserId;
                        user.FBAccessToken = model.ExternalAccessToken;
                    }
                    else
                    {
                        user.GoogleId = verifiedAccessToken.UserId;
                        user.GoogleAccessToken = model.ExternalAccessToken;
                    }

                    IdentityResult result = await this.authRepository.CreateAsync(user, model.UserRole.ToString());
                    if (!result.Succeeded)
                    {
                        string errorMessage = result.Errors.Any() ? result.Errors.First() : string.Empty;
                        return this.BadRequest(errorMessage);
                    }

                    var info = new ExternalLoginInfo
                    {
                        DefaultUserName = model.UserName,
                        Login = new UserLoginInfo(model.Provider, verifiedAccessToken.UserId)
                    };

                    result = await this.authRepository.AddLoginAsync(user.Id, info.Login);
                    if (!result.Succeeded)
                    {
                        return this.GetErrorResult(result);
                    }

                    userModel.ImageURL = ImageHelper.SaveImageFromUrl(userModel.ImageURL, user.Id);

                    if (model.UserRole == Roles.Company)
                    {
                        orgModel.AccountName = userModel.FirstName;
                        orgModel.Email1 = userModel.Email;
                        orgModel.FacebookURL = userModel.FBUrl;
                        orgModel.GooglePlusURL = userModel.GooglePlusURL;
                        orgModel.ImageURL = userModel.ImageURL;
                        orgModel = this.crmManagerService.AddOrganisation(orgModel, user);
                    }
                    else
                    {
                        userModel = this.crmManagerService.AddContact(userModel, user);
                    }
                }

                if (model.IsFromMobile)
                {
                    var accessTokenResponse = this.GenerateLocalAccessTokenResponse(user);
                    UserResultModel userResultModel = new UserResultModel();
                    OrgResultModel orgResultModel = new OrgResultModel();

                    CountryModel countryDet = this.commonService.GetUserCountryDetailsFromName(userModel.MailingCountry);

                    if (model.UserRole == Roles.Customer)
                    {
                        userResultModel = this.mapperFactory.GetMapper<ContactModel, UserResultModel>().Map(userModel);
                        userResultModel.ExternalAccessToken = accessTokenResponse;
                        userResultModel.Rank = 0.0M;
                        userResultModel.CountryDetails = countryDet;
                        userResultModel.UserRole = Roles.Customer;
                        userResultModel.IsActive = userModel.IsActive;
                        userResultModel.PaymentDetails = new PaymentModelDto()
                        {
                            PayPalId = userModel.PaypalId,
                            Mode = (PaymentMode)userModel.PaymentMode
                        };

                        MessagesDto message = new MessagesDto();
                        message.ModifiedBy = message.FromUser = message.CreatedBy = "YoufferAdmin";
                        message.IsDeleted = false;
                        message.CompanyId = AppSettings.Get<string>(ConfigConstants.SuperUserId);
                        message.UserId = message.ToUser = user.Id;
                        message.Name = "Youffer Admin";
                        message.MediaId = 0;

                        MessageTemplatesDto msgTemplate = this.commonService.GetMessageTemplate(MessageTemplateType.WelcomeMsg);
                        message.Message = msgTemplate.TemplateText;
                        this.youfferMessageService.CreateMessage(message);
                        int unreadMsgCount = this.youfferMessageService.GetUnreadMsgCount(message.UserId, false);

                        if (!string.IsNullOrEmpty(userResultModel.GCMId))
                        {
                            this.pushMessageService.SendMessageNotificationToAndroid(userResultModel.GCMId, message.Id.ToString(), message.Message, "Youffer", Notification.companymsg.ToString());
                        }

                        if (!string.IsNullOrEmpty(userResultModel.UDId))
                        {
                            this.pushMessageService.SendMessageNotificationToiOS(userResultModel.UDId, message.Id.ToString(), message.Message, "Youffer", unreadMsgCount, Notification.usermsg.ToString());
                        }

                        return this.Ok(userResultModel);
                    }
                    else
                    {
                        orgResultModel = this.mapperFactory.GetMapper<OrganisationModel, OrgResultModel>().Map(orgModel);
                        orgResultModel.ExternalAccessToken = accessTokenResponse.ToString();
                        orgResultModel.CountryDetails = countryDet;
                        orgResultModel.UserRole = Roles.Company;
                        orgResultModel.PaymentDetails = string.Empty;

                        return this.Ok(orgResultModel);
                    }
                }

                var redirectUri = string.Empty;
                var redirectUriValidationResult = this.ValidateClientAndRedirectUri(ref redirectUri);
                if (!string.IsNullOrEmpty(redirectUri))
                {
                    redirectUri = string.Format(
                                                  "{0}?external_access_token={1}&provider={2}&haslocalaccount={3}&external_user_name={4}",
                                                  redirectUri,
                                                  model.ExternalAccessToken,
                                                  model.Provider,
                                                  hasRegistered.ToString(),
                                                  model.UserName);
                    return this.Redirect(redirectUri);
                }

                externalLoginResultModel.ExternalAccessToken = model.ExternalAccessToken;
                externalLoginResultModel.Provider = model.Provider;
                externalLoginResultModel.HasLocalAccount = hasRegistered;
                externalLoginResultModel.ExternalUserName = model.UserName;
            }
            catch (Exception ex)
            {
                this.LoggerService.LogException("RegisterExternal : " + ex.Message);
            }

            return this.Ok(externalLoginResultModel);
        }
Exemplo n.º 2
0
        public async Task<IHttpActionResult> GetCompanyDetails(string companyId)
        {
            OrganisationModel orgModel = new OrganisationModel();

            orgModel = this.crmManagerService.GetOrganisation(companyId);
            CountryModel countryDet = this.commonService.GetCompanyCountryDetails(companyId);

            OrgResultModel orgResultModel = new OrgResultModel()
            {
                Id = companyId,
                Name = orgModel.AccountName,
                Email = orgModel.Email1,
                SecondaryEmail = orgModel.Email2,
                FacebookURL = orgModel.FacebookURL,
                GooglePlusURL = orgModel.GooglePlusURL,
                Website = orgModel.Website,
                Description = orgModel.Description,
                Phone = orgModel.Phone,
                OtherPhone = orgModel.OtherPhone,
                Fax = orgModel.Fax,
                AnnualRevenue = orgModel.AnnualRevenue,
                Employees = orgModel.Employees,
                CountryDetails = countryDet,
                Address = orgModel.BillAddress,
                City = orgModel.BillCity,
                State = orgModel.BillState,
                POBox = orgModel.BillPOBox,
                Timezone = orgModel.Timezone,
                Latitude = orgModel.Latitude,
                Longitude = orgModel.Longitude,
                ImageURL = orgModel.ImageURL,
                GCMId = orgModel.GCMId,
                UDId = orgModel.UDId,
                OSType = orgModel.OSType,
                UserRole = Roles.Company,
                Industry = orgModel.Industry,
                MainBusinessType = orgModel.MainBusinessType,
                SubBusinessType = orgModel.SubBusinessType,
                IsActive = orgModel.IsActive,
                PaymentDetails = string.Empty
            };

            return this.Ok(orgResultModel);
        }
Exemplo n.º 3
0
        public async Task<IHttpActionResult> GetMExternalLogin(RegisterExternalBindingModel model)
        {
            try
            {
                try
                {
                    this.LoggerService.LogException("MExternalLogin Request -json- " + JsonConvert.SerializeObject(model));
                }
                catch (Exception ex1)
                {
                }

                var verifiedAccessToken = await this.VerifyExternalAccessToken(model.Provider, model.ExternalAccessToken);
                if (verifiedAccessToken == null)
                {
                    return this.BadRequest("Invalid Provider or External Access Token");
                }

                var user = await this.authRepository.FindAsync(new UserLoginInfo(model.Provider, verifiedAccessToken.UserId));

                model.IsFromMobile = true;
                model.UserRole = (Roles)model.Role;
                model.OSType = (OSType)model.OS;

                ////string ipAddress = string.IsNullOrEmpty(model.IPAddress) ? HttpContext.Current.Request.UserHostAddress : model.IPAddress;
                ////this.LoggerService.LogException("IP Address from Request - " + ipAddress);
                ////CityDto cityData = this.ip2LocationService.GetCityData(ipAddress);
                ////model.Country = cityData.CountryInfo.CountryName;
                ////model.State = cityData.MostSpecificSubDivisionName == null ? string.Empty : cityData.MostSpecificSubDivisionName;
                ////model.Latitude = cityData.Latitude;
                ////model.Longitude = cityData.Longitude;

                bool hasRegistered = user != null;

                if (!hasRegistered)
                {
                    return await this.RegisterExternal(model);
                }

                AccessTokenModel accessTokenResponse = this.GenerateLocalAccessTokenResponse(user);
                CountryModel countryDet = this.commonService.GetUserCountryDetails(user.Id);

                if (model.UserRole == Roles.Customer)
                {
                    string crmContactId = this.userService.GetContact(user.Id).CRMId;
                    decimal rank = this.crmManagerService.GetUserRank(crmContactId);
                    string crmLeadId = string.Empty;
                    if (!string.IsNullOrEmpty(crmContactId))
                    {
                        crmLeadId = this.youfferContactService.GetMappingEntryByContactId(user.Id).LeadId;
                    }

                    ContactModel contactModel = new ContactModel();
                    contactModel = this.crmManagerService.GetContact(user.Id);
                    contactModel.IsOnline = true;
                    if (!string.IsNullOrWhiteSpace(model.GCMId))
                    {
                        contactModel.GCMId = model.GCMId;
                    }

                    if (!string.IsNullOrWhiteSpace(model.UDId))
                    {
                        contactModel.UDId = model.UDId;
                    }

                    contactModel = this.crmManagerService.UpdateContact(user.Id, contactModel);

                    LeadModel leadModel = new LeadModel();
                    UserResultModel userResultModel = new UserResultModel();

                    if (!string.IsNullOrEmpty(crmLeadId))
                    {
                        leadModel = this.crmManagerService.GetLead(crmLeadId);

                        userResultModel = this.mapperFactory.GetMapper<LeadModel, UserResultModel>().Map(leadModel) ?? new UserResultModel();
                        userResultModel.Id = user.Id;
                        userResultModel.ExternalAccessToken = accessTokenResponse;
                        userResultModel.Rank = rank;
                        userResultModel.CountryDetails = countryDet;
                        userResultModel.UserRole = Roles.Customer;
                        userResultModel.PaymentDetails = new PaymentModelDto()
                        {
                            PayPalId = leadModel.PaypalId,
                            Mode = (PaymentMode)leadModel.PaymentMode
                        };
                    }
                    else
                    {
                        userResultModel = this.mapperFactory.GetMapper<ContactModel, UserResultModel>().Map(contactModel) ?? new UserResultModel();
                        userResultModel.Id = user.Id;
                        userResultModel.ExternalAccessToken = accessTokenResponse;
                        userResultModel.CountryDetails = countryDet;
                        userResultModel.UserRole = Roles.Customer;
                        userResultModel.PaymentDetails = new PaymentModelDto()
                        {
                            PayPalId = contactModel.PaypalId,
                            Mode = (PaymentMode)contactModel.PaymentMode
                        };
                    }

                    return this.Ok(userResultModel);
                }
                else
                {
                    OrgResultModel orgResultModel = new OrgResultModel();
                    OrganisationModel orgModel = new OrganisationModel();

                    orgModel = this.crmManagerService.GetOrganisation(user.Id);
                    orgResultModel = this.mapperFactory.GetMapper<OrganisationModel, OrgResultModel>().Map(orgModel) ?? new OrgResultModel();
                    orgResultModel.Id = user.Id;
                    orgResultModel.ExternalAccessToken = accessTokenResponse.ToString();
                    orgResultModel.CountryDetails = countryDet;
                    orgResultModel.UserRole = Roles.Company;
                    orgResultModel.PaymentDetails = string.Empty;

                    return this.Ok(orgResultModel);
                }
            }
            catch (Exception ex)
            {
                this.LoggerService.LogException("GetMExternalLogin : " + ex.Message);
                return this.BadRequest(ex.Message);
            }
        }