Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Terrasoft.Configuration.UserWSService
- {
- using Newtonsoft.Json;
- using System;
- using System.ServiceModel;
- using System.ServiceModel.Activation;
- using System.ServiceModel.Web;
- using System.Web;
- using System.Text.RegularExpressions;
- using Terrasoft.Common;
- using Terrasoft.Core;
- using Terrasoft.Core.Configuration;
- using Terrasoft.Core.DB;
- using Terrasoft.Core.Entities;
- using Terrasoft.Core.Factories;
- using Terrasoft.Configuration;
- using Terrasoft.Core.Configuration;
- using Terrasoft.Configuration.QMUserManagementService;
- using global::Common.Logging;
- public class CreateUserResult
- {
- public bool Result;
- public string ErrorMessage;
- public Guid? ApplicationId;
- }
- public class GetAspAuthBySessionResult
- {
- public string AspAuth { get; set; }
- public string City { get; set; }
- public string RefSources { get; set; }
- public string DeviceId { get; set; }
- public string Error { get; set; }
- }
- [ServiceContract]
- [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
- public class UserWSService {
- private UserConnection _userConnection;
- private UserConnection UserConnection {
- get {
- if (_userConnection == null) {
- _userConnection = (UserConnection)HttpContext.Current.Session["UserConnection"];
- }
- return _userConnection;
- }
- }
- //private static readonly ILog log = LogManager.GetCurrentClassLogger();
- [OperationContract]
- [WebInvoke(Method = "POST", UriTemplate = "CreateAccount", BodyStyle = WebMessageBodyStyle.Wrapped,
- RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
- public string CreateAccount(string phoneNumber, string email, string passwordHash, string deviceId, string city, string offerSum, string offerPeriod,
- string tokenSurname, string tokenFirstName, string tokenAdditionalName, DateTime tokenBirthDate)
- {
- var log = LogManager.GetCurrentClassLogger();
- log.DebugFormat(string.Format("phoneNumber={0};email={1};passwordHash={2};deviceId={3};offerSum={4};offerPeriod={5};tokenSurname={6};tokenFirstName={7};tokenAdditionalName={8};tokenBirthDate={9};city={10}",
- phoneNumber, email,passwordHash,deviceId,offerSum,offerPeriod,tokenSurname,tokenFirstName,tokenAdditionalName,tokenBirthDate, city));
- CreateUserResult returnObject = new CreateUserResult(){
- Result = false,
- ErrorMessage = "Account Not Found"
- };
- try
- {
- if (tokenBirthDate.Kind == DateTimeKind.Utc) {
- tokenBirthDate = tokenBirthDate.ToLocalTime();
- }
- var noMiddleName = false;
- if (tokenAdditionalName == null || tokenAdditionalName == "") {
- noMiddleName = true;
- }
- var login = new Regex("[^+\\d]").Replace(phoneNumber, "");
- log.DebugFormat("login: {0}", login);
- var unitSelect = (new Select(UserConnection).Top(1)
- .From("SysAdminUnit")
- .Column("Id")
- .Where("Name").IsEqual(Column.Parameter(login))) as Select;
- var unitId = Guid.Empty;
- using (var dbExecutor = UserConnection.EnsureDBConnection())
- {
- using (var reader = unitSelect.ExecuteReader(dbExecutor))
- {
- while(reader.Read())
- {
- if(!reader.IsDBNull(reader.GetOrdinal("Id")))
- {
- unitId = reader.GetGuid(reader.GetOrdinal("Id"));
- }
- }
- }
- }
- log.DebugFormat("unitId: {0}", unitId);
- int iOfferSum = 5000;
- int iOfferPeriod = 9;
- Terrasoft.Configuration.Contact contact = null;
- if(unitId == Guid.Empty) {
- var name = string.Join(" ", tokenSurname, tokenFirstName, tokenAdditionalName);
- var number = new SysSettingsService.SysSettingsService().GetIncrementValueVsMask("ContactLastNumber", "ContactCodeMask");
- contact = new Terrasoft.Configuration.Contact(UserConnection);
- contact.SetDefColumnValues();
- contact.TypeId = QMConsts.Contact.Type.Borrower;
- contact.UniqueNumber = number;
- contact.Name = name;
- contact.FirstName = tokenFirstName;
- contact.SecondName = tokenSurname;
- contact.MiddleName = tokenAdditionalName;
- contact.CRBProcessingAgree = true;
- contact.TermsAgree = true;
- contact.PersonalDataProcessingAgree = true;
- contact.AccountId = QMConsts.Account.OurCompany;
- contact.BirthDate = tokenBirthDate;
- contact.MobilePhone = phoneNumber;
- contact.Email = email;
- contact.NoMiddleName = noMiddleName;
- contact.Save();
- log.DebugFormat("contact saved: {0}", contact.Id);
- var updateCommunications = new Update(UserConnection, "ContactCommunication")
- .Set("IsConfirmed", Column.Parameter(1))
- .Where("ContactId").IsEqual(Column.Parameter(contact.Id)) as Update;
- var rows = updateCommunications.Execute();
- log.DebugFormat("Communication updated {0} rows", rows);
- try
- {
- iOfferSum = (int)decimal.Parse(offerSum.Replace(".", ","));
- iOfferPeriod = int.Parse(offerPeriod);
- }
- catch (Exception ex)
- {
- log.Error(ex);
- log.DebugFormat("Error: {0}", ex.ToString());
- returnObject = new CreateUserResult()
- {
- Result = false,
- ErrorMessage = ex.ToString(),
- };
- return JsonConvert.SerializeObject(returnObject);
- }
- log.DebugFormat("iOfferSum={0};iOfferPeriod={1}", iOfferSum, iOfferPeriod);
- unitId = SecurityUtilities.CreateSSPAdminUnit(login, passwordHash, contact.Id, UserConnection);
- log.DebugFormat("unitId: {0}", unitId);
- }
- var cityStr = HttpUtility.UrlDecode(city);
- log.DebugFormat("cityStr: {0}", cityStr);
- var timeZone = GetTimeZone(HttpUtility.UrlDecode(city));
- var unit = new Terrasoft.Configuration.SysAdminUnit(UserConnection);
- unit.FetchFromDB(unitId);
- unit.TimeZoneId = timeZone != null && timeZone != string.Empty ? timeZone : "Russian Standard Time";
- unit.HomePageId = (Guid)Core.Configuration.SysSettings.GetValue(UserConnection, "SSPHomePageId");
- unit.Save(false);
- log.Debug("SysAdminUnit updated");
- log.DebugFormat("contact is null ? : {0}", contact == null);
- GrantContactRights(unitId, contact.Id);
- var applicationId = CreateContactLoan(contact.Id, deviceId, iOfferSum.ToString(), iOfferPeriod.ToString(), cityStr == null || cityStr == string.Empty ? "Неизвестный (IP: )" : cityStr);
- log.DebugFormat("created application: {0}", applicationId);
- GrantApplicationRights(unitId, new Guid(applicationId));
- returnObject = new CreateUserResult(){
- Result = true,
- ApplicationId = new Guid(applicationId)
- };
- }
- catch (Exception ex)
- {
- log.DebugFormat("Error: {0}", ex.ToString());
- returnObject = new CreateUserResult(){
- Result = false,
- ErrorMessage = ex.ToString()
- };
- }
- return JsonConvert.SerializeObject(returnObject);
- }
- private string GetTimeZone(string city)
- {
- var log = LogManager.GetCurrentClassLogger();
- log.DebugFormat("GetTimeZone: {0}", city);
- try
- {
- var citySelect = (new Select(UserConnection).Top(1)
- .From("KladrCities")
- .Column("Code")
- .Where("Name").IsEqual(Column.Parameter(city))) as Select;
- try
- {
- var regionCode = string.Empty;
- using (var dbExecutor = UserConnection.EnsureDBConnection())
- {
- using (var reader = citySelect.ExecuteReader(dbExecutor))
- {
- while(reader.Read())
- {
- if(!reader.IsDBNull(reader.GetOrdinal("Code")))
- {
- regionCode = reader.GetString(reader.GetOrdinal("Code")).Substring(0, 2);
- log.DebugFormat("GetTimeZone. regionCode: {0}", regionCode);
- }
- }
- }
- }
- var zoneSelect = (new Select(UserConnection).Top(1)
- .From("KladrRegionTimeZone").As("regToZone")
- .Column("zone", "Code")
- .LeftOuterJoin("TimeZone").As("zone").On("zone", "Id").IsEqual("regToZone", "TimeZoneId")
- .Where("regToZone", "RegionCode").IsEqual(Column.Parameter(regionCode))) as Select;
- using (var dbExecutor = UserConnection.EnsureDBConnection())
- {
- using (var reader = zoneSelect.ExecuteReader(dbExecutor))
- {
- while(reader.Read())
- {
- if(!reader.IsDBNull(reader.GetOrdinal("Code")))
- {
- var zoneCode = reader.GetString(reader.GetOrdinal("Code"));
- log.DebugFormat("GetTimeZone. zoneCode: {0}", zoneCode);
- return zoneCode;
- }
- }
- }
- }
- }
- catch(Exception e)
- {
- log.DebugFormat("Error: {0}", e.ToString());
- return string.Empty;
- }
- }
- catch (Exception ex)
- {
- log.DebugFormat("Error: {0}", ex.ToString());
- return string.Empty;
- }
- return string.Empty;
- }
- private string CreateContactLoan(Guid contactId, string deviceId, string offerSum, string offerPeriod, string city) {
- var applicationService = new QuickMoneyApplicationWebServicesEx.QuickMoneyApplicationWebServicesEx();
- var result = applicationService.CreateApplication(contactId, deviceId, city);
- if (!result.success ||
- (string.IsNullOrEmpty(deviceId) && string.IsNullOrEmpty(offerSum) && string.IsNullOrEmpty(offerPeriod))) {
- return result.applicationId.ToString();
- }
- var applicationSchema = UserConnection.EntitySchemaManager.GetInstanceByName("Application");
- var application = applicationSchema.CreateEntity(UserConnection);
- application.FetchFromDB(result.applicationId);
- application.SetColumnValue("OfferPeriod", offerPeriod);
- application.SetColumnValue("OfferSum", offerSum);
- application.Save();
- return result.applicationId.ToString();
- }
- private void GrantContactRights(Guid unitId, Guid contactId) {
- using (var dbExecutor = UserConnection.EnsureDBConnection()) {
- for (var operationNumber = 0; operationNumber < 2; ++operationNumber) {
- var insert = new Insert(UserConnection)
- .Into("SysContactRight")
- .Set("RecordId", Column.Parameter(contactId))
- .Set("SysAdminUnitId", Column.Parameter(unitId))
- .Set("Position", Column.Parameter(0))
- .Set("SourceId", Column.Parameter(QMConsts.SysEntitySchemaRecRightSource.Owner))
- .Set("Operation", Column.Parameter(operationNumber))
- .Set("RightLevel", Column.Parameter(2));
- insert.Execute(dbExecutor);
- }
- }
- }
- private void GrantApplicationRights(Guid unitId, Guid recordId) {
- var schema = UserConnection.EntitySchemaManager.GetInstanceByName("Application");
- if (!schema.AdministratedByRecords) {
- return;
- }
- using (var dbExecutor = UserConnection.EnsureDBConnection()) {
- for (var operationNumber = 0; operationNumber <= 1; operationNumber++) {
- var insert = new Insert(UserConnection)
- .Into("SysApplicationRight")
- .Set("RecordId", Column.Parameter(recordId))
- .Set("SysAdminUnitId", Column.Parameter(unitId))
- .Set("Position", Column.Parameter(0))
- .Set("SourceId", Column.Parameter(QMConsts.SysEntitySchemaRecRightSource.Owner))
- .Set("Operation", Column.Parameter(operationNumber))
- .Set("RightLevel", Column.Parameter(1));
- insert.Execute(dbExecutor);
- }
- }
- }
- [OperationContract]
- [WebInvoke(Method = "POST", UriTemplate = "LogIn", BodyStyle = WebMessageBodyStyle.Wrapped,
- RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
- public string LogIn(string userName, string userPassword, string workspaceName, int timeZoneOffset, string ipAddress, string agent){
- var returnObject = "OK";
- try
- {
- string sessionId = Guid.NewGuid().ToString();//Terrasoft.Web.Common.SessionHelper.GetSessionId();
- var userCon = UserConnectionFactory.CreateUserConnection(
- UserConnection.AppConnection,
- new AuthData(){
- UserName = userName,
- UserPassword = userPassword,
- WorkspaceName = workspaceName,
- TimeZoneOffset = timeZoneOffset
- },
- sessionId,
- ipAddress,
- agent);
- return sessionId;
- }
- catch (Exception ex)
- {
- returnObject = "Error. " + ex.ToString();
- }
- return returnObject;
- }
- [OperationContract]
- [WebInvoke(Method = "POST", UriTemplate = "GetAspAuthBySession", BodyStyle = WebMessageBodyStyle.Wrapped,
- RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
- public string GetAspAuthBySession(string sessionId){
- var returnObject = new GetAspAuthBySessionResult();
- try
- {
- var userSessionSelect = (new Select(UserConnection).Top(1)
- .From("UserSessionTable")
- .Column("AspAuth")
- .Column("City")
- .Column("RefSources")
- .Column("DeviceId")
- .Where("SessionId").IsEqual(Column.Parameter(sessionId))) as Select;
- try
- {
- using (var dbExecutor = UserConnection.EnsureDBConnection())
- {
- using (var reader = userSessionSelect.ExecuteReader(dbExecutor))
- {
- while(reader.Read())
- {
- if(!reader.IsDBNull(reader.GetOrdinal("AspAuth")))
- returnObject.AspAuth = reader.GetString(reader.GetOrdinal("AspAuth"));
- if(!reader.IsDBNull(reader.GetOrdinal("City")))
- returnObject.City = reader.GetString(reader.GetOrdinal("City"));
- if(!reader.IsDBNull(reader.GetOrdinal("RefSources")))
- returnObject.RefSources = reader.GetString(reader.GetOrdinal("RefSources"));
- if(!reader.IsDBNull(reader.GetOrdinal("DeviceId")))
- returnObject.DeviceId = reader.GetString(reader.GetOrdinal("DeviceId"));
- return JsonConvert.SerializeObject(returnObject);
- }
- }
- }
- }
- catch(Exception e)
- {
- returnObject.Error = e.ToString();
- return JsonConvert.SerializeObject(returnObject);
- }
- }
- catch (Exception ex)
- {
- returnObject.Error = ex.ToString();
- return JsonConvert.SerializeObject(returnObject);
- }
- returnObject.Error = "NotFound";
- return JsonConvert.SerializeObject(returnObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement