예제 #1
0
        public ActionResult Reset(ResetModel model)
        {
            string message;

            // Email address must be specified.
            if (!string.IsNullOrWhiteSpace(model.Email))
            {
                // Get all accounts with that email address; check if there are any.
                var accounts = new LinqMetaData().User.Where(u => u.EmailAddress == model.Email);
                if (accounts.Any())
                {
                    // Have some accounts. Filter out all but the unrestricted ones.
                    accounts = GetUnrestictedOnly(accounts, model.UserName, out message);
                    if (accounts.Any())
                    {
                        // Have unrestricted accounts. They will be reset. Assume there's
                        // more than one. If more than one, we don't necessarily know the
                        // person's name.
                        string firstName = null;
                        string lastName  = null;
                        if (accounts.Count() == 1)
                        {
                            // Only one. So get person's name.
                            var account = accounts.First();
                            firstName = account.FirstName;
                            lastName  = account.LastName;
                        }

                        // Create restriction on each account, send email and tell user it's done.
                        var key = CreateRestriction(accounts, model.Email, firstName != null);
                        SendResetEmail(model.Email, key, firstName, lastName);
                        return(RedirectToAction("ResetSuccess"));
                    }
                }
                else
                {
                    // No accounts with the specified email address. Tell user.
                    message = Account.Invalid_EmailNotMatched;
                }
            }
            else
            {
                // No email address. Tell user to enter one.
                message = Account.Invalid_EmailNotSpecified;
            }

            ModelState.AddModelError("", message);

            // If we got this far, something failed, redisplay form.
            return(View());
        }
예제 #2
0
        public void Users_Must_Have_One_Role()
        {
            // check all the users in the database for one role
            var multiRoles = new LinqMetaData().User.Where(x => x.Roles.Count() != 1);

            Assert.IsTrue(!multiRoles.Any());

            // there is no other tests we can do because there is no way to put multiple roles in to the system
        }
        public void Device_Registration_Requires_User_Account()
        {
            Create_Edit_Device(TestData.ServiceAdminUsername, true);
            var device = new LinqMetaData().Device.FirstOrDefault(x => x.SerialNumber == "DeviceTest1");

            Assert.IsNotNull(device);
            // invoke to setup http and everything
            // TODO: do this for Webs services directly
            var controller = Mock();

            controller.HttpContext.User = new RolePrincipal(new GenericIdentity(TestData.ServiceAdminUsername));
            controller.Invoke(x => x.Edit(device.DeviceId, new DeviceModel(device.DeviceId)
            {
                DeviceState = DeviceState.Transitioning
            }));

            var registration = new RegistrationService();

            registration.GetLocations();

            // test that all devices have a location
            var invalid = new LinqMetaData().Device.Where(x => x.Location == null);

            Assert.IsFalse(invalid.Any());

            // test that a device cannot be registered by a user outside of the organization
            var outsideUser =
                new LinqMetaData().User.FirstOrDefault(x => x.OrganizationId != device.Location.OrganizationId);

            Assert.IsNotNull(outsideUser);
            HttpContext.Current.User = new RolePrincipal(new GenericIdentity(outsideUser.Username));
            Thread.CurrentPrincipal  = HttpContext.Current.User;
            try
            {
                registration = new RegistrationService();
                registration.RegisterDevice(device.Location.UniqueIdentifier, device.SerialNumber);
                Assert.Fail("User outside organization cannot register device.");
            }
            catch (WebFaultException <string> ex)
            {
                Assert.AreEqual(HttpStatusCode.PreconditionFailed, ex.StatusCode);
                Assert.AreEqual(Constants.StatusSubcode.LOCATION_INVALID, ex.Detail);
            }
        }