Example #1
0
    private void DeleteInvitation(RegisterBindingModel model)
    {
      Guid invitationId = Guid.Parse(model.InvitationId);

      Invitation invitation = _unitOfWork.InvitationRepository.Get(i => i.InvitationId == invitationId).
        First();
      _unitOfWork.InvitationRepository.Delete(invitation);

    }
Example #2
0
    private NetworkCompany CreateCompany(RegisterBindingModel model, Network network)
    {
      ICollection<Survey> surveys = CreateSurveys(network);

      var address = new Address{
                                 AddressId = Guid.NewGuid(),
                                 Street = model.Street,
                                 PostalCode = model.PostalCode,
                                 City = model.City,
                                 Website = model.Website,
                                 Lat = model.Lat,
                                 Lon = model.Lon
                               };

      var company = new Company{
                                 CompanyId = Guid.NewGuid(),
                                 Name = model.CompanyName,
                                 Address = address
                               };
      
      return new NetworkCompany{
                                                                   NetworkCompanyId = Guid.NewGuid(),
                                                                   Company = company,
                                                                   Surveys = surveys,
                                                                   Network = network
                                                                 };
    }
Example #3
0
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
      if (!ModelState.IsValid)
      {
        return BadRequest(ModelState);
      }

      var user = new UserProfile
      {
        UserName = model.UserName,
        FirstName = model.FirstName,
        LastName = model.LastName,
        PhoneNumber = model.PhoneNumber,
        Email = model.Email,
        EmailConfirmed = false,
      };

      if (model.NetworkId != null)
      {
        Guid networkId = Guid.Parse(model.NetworkId);
        Network network = _unitOfWork.NetworkRepository.First(n => n.NetworkId == networkId, "NetworkCompanies");
        NetworkCompany nc = CreateCompany(model, network);
        network.NetworkCompanies.Add(nc);
        user.CompanyId = nc.Company.CompanyId;
      }
      else if (model.CompanyId != null)
      {
        user.CompanyId = Guid.Parse(model.CompanyId);
      }

      _unitOfWork.Commit();

      IdentityResult identityResult = await UserManager.CreateAsync(user,
        model.Password);
      
      if (identityResult.Succeeded && (model.NetworkId != null || model.CompanyId != null))
      {
        UserManager.AddToRole(user.Id,
          "User");
      }

      IHttpActionResult createResult = GetErrorResult(identityResult);

      if (createResult != null)
      {
        return createResult;
      }

      UserProfile justCreatedUser = await UserManager.FindByNameAsync(model.UserName);

      string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

      string callbackUrl = Url.Link("ConfirmEmail",
        new{
             userId = user.Id,
             code
           });

      var notification = new AccountNotificationModel{
                                                       Code = code,
                                                       Url = callbackUrl,
                                                       UserId = justCreatedUser.Id,
                                                       Email = justCreatedUser.Email,
                                                       DisplayName = justCreatedUser.UserName
                                                     };

      string body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml",
        notification);

      await UserManager.SendEmailAsync(user.Id,
        "EnergieNetz: Konto aktivieren",
        body);

      DeleteInvitation(model);

      return Ok();
    }