WiX Cookbook
上QQ阅读APP看书,第一时间看更新

Changing the permissions on a file for a user

Although setting access at the folder level is good for quickly securing all of your files in one fell swoop, we can still fine-tune permissions on a file-by-file basis. This might be useful, for example, if you have a main executable that can be run by anyone but another administrative utility that should only be run by certain users.

In this recipe, we'll install a text file and update its permissions so that our user Joe has full access.

Getting ready

To prepare for this recipe, perform the following steps:

  1. Create a new setup project and name it FilePermissionsInstaller.
  2. Add a text file named Sample.txt to the project and then add a Component element to include it in the installation:
    <ComponentGroup Id="ProductComponents"
                    Directory="INSTALLFOLDER">
      <Component Id="cmpSampleTXT"
                 Guid="{FB746118-B1E5-42DC-AA76-862C4E1EABCF}">
        <File Source="Sample.txt">
        </File>
      </Component>
    </ComponentGroup>
  3. The user for whom we are setting permissions must already exist. Manually, create a user named Joe on the target computer by right-clicking on This PC and going to Manage | Local Users and Groups. Right-click on the Users node and select New User. Set the user's name as Joe.

How to do it…

As shown in the following steps, include util:PermissionEx inside a File element to set permissions on a file:

  1. Reference UtilExtension by right-clicking on the References node in Solution Explorer, going to Add Reference… | Browse, and then adding WixUtilExtension.dll.
  2. Add the UtilExtension namespace to the Wix element in Product.wxs:
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" 
    xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  3. Find the File element that you want to set permissions on and then add a util:PermissionEx element inside it. Specify the user to whom you'd like to give access and the type of permissions to allow. The following code grants full access to the Sample.txt file for a user named Joe:
    <File Source="Sample.txt">
       <util:PermissionEx User="Joe" GenericAll="yes"/>
    </File>

How it works…

We began by adding a reference to WixUtilExtension.dll and including the UtilExtension namespace, http://schemas.microsoft.com/wix/UtilExtension, in the Wix element. This gives us access to the PermissionEx element.

By putting PermissionEx inside a File element, we're saying that we want to update the permissions on this particular file after we've installed it. The User attribute selects the user to give access to and the GenericAll attribute gives them read, write, and execute permissions. You can learn more about the other attributes that are available at http://wixtoolset.org/documentation/manual/v3/xsd/util/permissionex.html.

You can view the permissions on a file by opening its properties and selecting the Security tab:

How it works…

Here, a user named Joe has been given full access to read, write, and modify the file.