예제 #1
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The Dfa user object running the code example.
        /// </param>
        public override void Run(DfaUser user)
        {
            // Create UserRemoteService instance.
            UserRemoteService service = (UserRemoteService)user.GetService(
                DfaService.v1_20.UserRemoteService);

            long userId       = long.Parse(_T("INSERT_USER_ID_HERE"));
            long advertiserId = long.Parse(_T("INSERT_ADVERTISER_ID_HERE"));

            // Create and configure a user filter.
            UserFilter filterToAdd = new UserFilter();

            // The following field has been filled in to make a filter that allows a
            // user to access only the assigned objects. This value was determined
            // using GetUserFilterTypes.cs.
            filterToAdd.userFilterCriteriaId = 2;

            // Because this filter used the criteria type "Assigned" it is necessary
            // to specify what advertisers this user has access to. This next step
            // would be skipped for the criteria types "All" and "None".

            // Create an object filter to represent each object the user has access
            // to. Since this is an advertiser filter, an object filter represents an
            // advertiser. The total of object filter objects will need to match the
            // total of advertisers the user is assigned.
            ObjectFilter allowedObject = new ObjectFilter();

            // Insert the advertiser id of an advertiser assigned to this user.
            allowedObject.id = advertiserId;

            // Create any additional object filters that are needed, then add these
            // settings to the user filter
            filterToAdd.objectFilters = new ObjectFilter[] { allowedObject };

            try {
                // Retrieve the user who is to be modified.
                User userToModify = service.getUser(userId);

                // Add the filter to the user. The following method is specific to
                // advertiser filters. See the User class documentation for the names of
                // methods for other filters.
                userToModify.advertiserUserFilter = filterToAdd;

                // Save the changes made and display a success message.
                UserSaveResult userSaveResult = service.saveUser(userToModify);
                Console.WriteLine("User with id \"{0}\" was modified.", userSaveResult.id);
            } catch (Exception ex) {
                Console.WriteLine("Failed to add advertiser user filter. Exception says \"{0}\"",
                                  ex.Message);
            }
        }
        //You might want to create a company-specific user login for the company in question.
        private static void AddUser(AccountSvc svc, int companyId, int accountId)
        {
            User newUser = new User();
            newUser.CompanyId = companyId;
            newUser.AccountId = accountId;

            newUser.Email = "*****@*****.**";//Should be merchant/client user email
            newUser.FirstName = "Anya";
            newUser.LastName = "Stettler";
            newUser.UserName = "******"+DateTime.Now; //Must be unique within the Avalara server - we usually use email address. I've added a timestamp here for testing.
            newUser.PostalCode = "98110"; //Not required, but improves user experience (as postal code is required to perform a password reset)

            newUser.SecurityRoleId = SecurityRoleId.CompanyAdmin; //This will give the user access to the specified company ONLY, and will allow them to change profile settings, etc. for that company.
            newUser.IsActive = true;

            UserSaveResult res = new UserSaveResult();
            try
            {
                res = svc.UserSave(newUser); //Save the user
                if (res.ResultCode.Equals(SeverityLevel.Success))
                {
                    Console.WriteLine("User saved successfully. UserId: " + res.UserId);
                }
                else
                {
                    Console.WriteLine("Error when saving user. Error: " + res.Messages[0].Summary);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in UserSave: " + ex);
            }

            //By default, a new user is assigned a password (which is emailed to them). They are required to change this password upon first login to the admin console.
            //If you want to eliminate all contact with the Admin Console, you'll need to reset the password programatically.
            //Note: this kicks off an email to the user email address notifying them that someone has changed their account.
            newUser.UserId = res.UserId;
            newUser.PasswordStatusId = PasswordStatusId.UserCanChange; //This removes the "must change" property
            newUser.Password = "******";
            res = svc.UserSave(newUser);
        }