all files / src/ sputils.user.js

100% Statements 19/19
100% Branches 0/0
100% Functions 9/9
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                                                                  
(function () {
  /**
   * @function sputils.user.loginAsAnotherUser
   * @returns {void}
   * @example
   * sputils.user.loginAsAnotherUser();
   */
  var loginAsAnotherUser = function () {
    global.location.href = '/_layouts/closeconnection.aspx?loginasanotheruser=true';
  };
 
  /**
   * @function sputils.user.logoutUser
   * @returns {void}
   * @example
   * sputils.user.logoutUser();
   */
  var logoutUser = function () {
    global.location.href = '/_layouts/closeconnection.aspx';
  };
 
 
  /**
   * Returns a promise with the current spuser object.
   * @function sputils.user.getCurrentUser
   * @returns {Promise<object>} the promise with the user object
   * @example
   * sputils.user.getCurrentUser().then(function (data) {
   *   console.log(data);
   */
  var getCurrentUser = function () {
    return new Promise(function (resolve, reject) {
      var clientContext = new SP.ClientContext.get_current();
      var web = clientContext.get_web();
      var currentUser = web.get_currentUser();
 
      // Load currentUser to the context in order to retrieve the user data.
      clientContext.load(currentUser);
 
      // Execute the query. Takes two functions: success and failed
      // that returns the SPUser object or an error message.
      clientContext.executeQueryAsync(function () {
        resolve(currentUser);
      }, function (sender, args) {
        reject(new Error(args.get_message()));
      });
    });
  };
 
  /**
   * Returns full url to profile page of current user
   * @function sputils.user.getCurrentUserPersonalSiteUrl
   * @param {object} config - an object containing config for the REST call
   * @returns {Promise<object>}
   * @example
   *  sputils.user.getCurrentUserPersonalSiteUrl()
   *    .then(function (data) { console.log(data) });
   */
  var getCurrentUserPersonalSiteUrl = function (config) {
    var url =
          '/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=UserUrl';
    return sputils.rest.get(url, config)
      .then(function (data) {
        return data.d.UserUrl;
      });
  };
 
  /** @namespace */
  sputils.user = {
    loginAsAnotherUser: loginAsAnotherUser,
    logoutUser: logoutUser,
    getCurrentUser: getCurrentUser,
    getCurrentUserPersonalSiteUrl: getCurrentUserPersonalSiteUrl
  };
})();