예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SmartHomeWeb.Model.Person"/> class.
 /// </summary>
 /// <param name="Guid">The person's globally unique identifier.</param>
 /// <param name="Data">A data structure that describes the person.</param>
 public Person(Guid Guid, PersonData Data)
 {
     this.Guid = Guid;
     this.Data = Data;
 }
        private async Task<object> PostAddPerson(dynamic parameters, CancellationToken ct)
        {
            ViewBag.Success = "";
            ViewBag.Error = "";

            string username = FormHelpers.GetString(Request.Form, "personusername");
            string name = FormHelpers.GetString(Request.Form, "personname");
            string password = FormHelpers.GetRawString(Request.Form, "personpassword");
            string repeatPassword = FormHelpers.GetRawString(Request.Form, "personpasswordrepeat");
            string address = FormHelpers.GetString(Request.Form, "personaddress");
            DateTime? birthdate = FormHelpers.GetDate(Request.Form, "personbirthdate");
            string city = FormHelpers.GetString(Request.Form, "personcity");
            string zipcode = FormHelpers.GetString(Request.Form, "personzipcode");

            if (string.IsNullOrWhiteSpace(username))
            {
                ViewBag.Error = TextResources.EmptyUserNameError;
            }
            else if (string.IsNullOrWhiteSpace(name))
            {
                ViewBag.Error = TextResources.EmptyNameError;
            }
            else if (string.IsNullOrWhiteSpace(password))
            {
                ViewBag.Error = TextResources.EmptyPasswordError;
            }
            else if (password != repeatPassword)
            {
                ViewBag.Error = TextResources.PasswordMismatchError;
            }
            else if (!birthdate.HasValue)
            {
                ViewBag.Error = TextResources.BadlyFormattedBirthDateError;
            }
            else
            {
                // DataConnection.Ask is used here, because it conveniently wraps everything in
                // a single transaction.
                await DataConnection.Ask(async dc =>
                {
                    var sender = await dc.GetPersonByUsernameAsync(username);
                    if (sender != null)
                    {
                        ViewBag.Error = string.Format(TextResources.PersonAlreadyExistsError, username);
                    }
                    else
                    {
                        var personData = new PersonData(username, password, name, birthdate.Value, address, city, zipcode, false);
                        // Create the person
                        await dc.InsertPersonAsync(personData);
                        ViewBag.Success = TextResources.AddedPersonMessage;
                    }
                });
            }

            if (string.IsNullOrWhiteSpace((string)ViewBag.Error))
            {
                UserIdentity user;
                UserMapper.FindUser(username, password, out user);
                // Everything went fine. Log in and redirect to the profile page.
                return this.LoginAndRedirect(user.Guid, DateTime.Now.AddYears(1), "/person/" + username);
            }
            else
            {
                return await GetAddPerson(parameters, ct);
            }
        }