Adaptive User Management

。_饼干妹妹 提交于 2019-12-23 05:10:37

问题


I have built a review app based on Google's "people viewer" template that allows managers to create and edit reviews for their direct reports.

  • The app contains the directory model as well as three roles: Admins, HR, EndUsers.
  • The app contains a user settings model that allows to create and store user settings similar to the "people skills" template.
  • The app contains a review model that will contain the reviews for every employee. As one employee can have several reviews, this will be a one-to-many relation, either linked to directory model or user settings model.

The reviews should be readable by managers chain of manager. For this I have created a server script, assuming that the EmployeeEmail will be additionally stored in the review. But maybe there is a better alternative?

function getDirectReportsChainForUser_(query) {
  var userQuery = app.models.Directory.newQuery();
  userQuery.filters.PrimaryEmail._equals = query.parameters.PrimaryEmail;

  userQuery.prefetch.DirectReports._add();
  userQuery.prefetch.DirectReports.DirectReports._add();

  var users = userQuery.run();

  if (users.length === 0) {
   return [];
  }

  var user = users[0];
  var directs = user.DirectReports;
  var records = [];
  for (var i = 0; i <= directs.length; i++) {
    records.push(directs[i].PrimaryEmail);
  }
  // The following lines are based on the asumption that the EmployeeEmail 
  // will be stored in the review in case that there is no better alternative. 
  //The question that then remains is how to recursively add the DirectReports 
  //of the DirectReports to the array???
  var reviewQuery = app.models.Reviews.newQuery();
  reviewQuery.filters.EmployeeEmail._in = records; 
 return reviewQuery.run();
}

The manager should be able to define whether one or more of his deputies can read the reviews for his unit, too. My idea was to solve this issue through a many-to-many relation between the directory and review model, but I am not sure how to implement it?

Furthermore, once a manager or his deputy departures, it should be possible for the Admin to dissolve the connection and to reconnect the reviews to a successor. Therefore I was thinking about integrating a multiselect in the admin page. Would this be feasible?


回答1:


Here I see at least two distinct questions:

is there better way to associate directory model's record and ordinary data model than just adding primary email field to the data model

Nope, at this time it is not possible to establish relations between data (SQL/Drive Tables) and directory models.

how to recursively get all direct reports for a user

App Maker's Directory Model is a wrapper on top of G Suit Admin SDK's Directory API that exposes just a small subset of its powerful features. When you add Directory Model App Maker automatically plugs in correspondent Apps Script advance service:

Since we already have configured Directory API we can unleash its full power and easily fetch all manger's subordinates with a single call (or multiple if you have a need to support paging). In order to do that we will use Users.List API method with managerId query parameter (the only one that allows us to query all subordinates down the tree). Here are reference for the minimal set of search query parameters quoted from the full search documentation (without those parameters query would not work or wouldn't work in a way we need):

  • managerId: The ID of a user's manager either directly or up the management chain.
  • domain: The domain name. Use this field to get fields from only one domain. To return all domains for a customer account, use the customer query parameter instead. Either the customer or the domain parameter must be provided.
  • viewType: Whether to fetch the administrator-only or domain-wide public view of the user. For more information, see Retrieve a user as a non-administrator (admin_view is default value so we need to override it with domain_view).
  • query: Query string for searching user fields. For more information on constructing user queries, see Search for Users.
/**
 * Fetches all reviews associated with all subordinate employees (both direct
 * and indirect reports).
 */
function getAllReportsEmails(managerId) {
  var emails = [];
  var result = AdminDirectory.Users.list({
    domain: 'ENTER HERE YOUR DOMAIN (exapmle.com)',
    query: 'managerId=' + managerId,
    viewType: 'domain_public',
    maxResults: 100
  });

  if (result.users) {
    emails = result.users.map(function (user) {
      return user.primaryEmail;
    });
  }

  return emails;
}


/**
 * Fetches all reviews associated with all subordinate employees (both direct
 * and indirect reports).
 */
function getAllReportsReviewsForManager_(query) {
  var userQuery = app.models.Directory.newQuery();
  // For better security I would recommend to use
  // Session.getActiveUser().getEmail() instead of parameter
  // passed from the client.
  userQuery.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();

  var users = userQuery.run();

  if (users.length === 0) {
   return [];
  }

  var manager = users[0];
  var managerId = manager._key;
  var allReportsEmails = getAllReportsEmails(managerId);

  var reviewQuery = app.models.Reviews.newQuery();
  reviewQuery.filters.EmployeeEmail._in = allReportsEmails; 

  return reviewQuery.run();
}



回答2:


Pavel, I tried to integrate the ideas you gave me into one server script that returns an array of the manager and his whole subordinate chains (direct reports + indirect reports), so that I can use it whenever needed. I turned into a recursive function to get the direct reports and indirect reports on the next lower level. Is there a way to get the whole chain?

function getSubordinatesChainForUser(query) {
  var userQuery = app.models.Directory.newQuery();
  userQuery.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();
  userQuery.prefetch.DirectReports._add();
  userQuery.prefetch.DirectReports.DirectReports._add();
  var users = userQuery.run();
  if (users.length === 0) {
    return [];
  }  

  var userEmails = users.map(function(manager){
    var employeeEmails = manager.DirectReports.map(function(employee){
      return employee.PrimaryEmail;
    });
    return manager.PrimaryEmail + ',' + employeeEmails;
  });

  return userEmails;
}


来源:https://stackoverflow.com/questions/49506812/adaptive-user-management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!