Class: RallyUserManagement::UserHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/rally_user_helper.rb

Constant Summary collapse

ADMIN =

Setup constants

'Admin'
USER =
'User'
PROJECTADMIN_READ =

Different READ and CREATE attributes are imposed by WSAPI Hopefully this is a temporary hack

ADMIN
PROJECTADMIN_CREATE =
'Project Admin'
EDITOR =
'Editor'
VIEWER =
'Viewer'
NOACCESS =
'No Access'
TEAMMEMBER_YES =
'Yes'
TEAMMEMBER_NO =
'No'
SUB_CACHE =

Parameters for cache files

"_cached_subscription.txt"
WORKSPACE_CACHE =
"_cached_workspaces.txt"
PROJECT_CACHE =
"_cached_projects.txt"
CACHE_COL_DELIM =
"\t"
CACHE_ROW_DELIM =
"\n"
CACHE_WRITE_MODE =
"wb"
SUB_CACHE_FIELDS =
%w{SubscriptionID Name}
WORKSPACE_CACHE_FIELDS =
%w{ObjectID Name State}
PROJECT_CACHE_FIELDS =
%w{ObjectID ProjectName State WorkspaceName WorkspaceOID}

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ UserHelper

Returns a new instance of UserHelper.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rally_user_helper.rb', line 54

def initialize(config)

  @rally                     = config[:rally_api]
  @rally_json_connection     = @rally.rally_connection
  @logger                    = config[:logger]
  @create_flag               = config[:create_flag]
  @cached_users              = {}
  @cached_sub_id             = 0
  @cached_subscription       = {}
  @cached_workspaces         = {}
  @cached_projects           = {}
  @cached_workspaces_by_name = {}
  @cached_projects_by_name   = {}

  # Provides lookup of projects per workspace
  @workspace_hash_of_projects = {}

  # Maximum age in days of the Workspace/Project cache before refresh
  @max_cache_age = config[:max_cache_age] || 1

  # upgrade_only_mode - when running in upgrade_only_mode, check existing permissions
  # first, and only apply the change if it represents an upgrade over existing permissions
  @upgrade_only_mode = config[:upgrade_only_mode] || false

  # File encoding format
  @file_encoding = config[:file_encoding] || "US-ASCII"

  # User filter for ENABLED users only
  # For purposes of speed/efficiency, summarize Enabled Users ONLY
  @summarize_enabled_only = true
  @enabled_only_filter = "(Disabled = \"False\")"

  # fetch data
  @initial_user_fetch            = "UserName,FirstName,LastName,DisplayName"
  @detail_user_fetch             = "UserName,FirstName,LastName,DisplayName,UserPermissions,Name,Role,Workspace,ObjectID,Project,ObjectID,TeamMemberships,UserProfile"

end

Instance Method Details

#cache_project_entry(header, row) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/rally_user_helper.rb', line 599

def cache_project_entry(header, row)
  project_id                   = row[header[0]].strip
  project_name                 = row[header[1]].strip
  project_state                = row[header[2]].strip
  project_workspace_name       = row[header[3]].strip
  project_workspace_oid        = row[header[4]].strip

  this_project = {}
  this_project["ObjectID"]     = project_id
  this_project["Name"]         = project_name
  this_project["State"]        = project_state
  this_project["_ref"]         = make_ref_from_oid("project", project_id)
  this_workspace               = {}
  this_workspace["Name"]       = project_workspace_name
  this_workspace["ObjectID"]   = project_workspace_oid
  this_workspace["_ref"]       = make_ref_from_oid("workspace", project_workspace_oid)
  this_project["Workspace"]    = this_workspace
  @cached_projects[project_id] = this_project

  if !@cached_projects_by_name.has_key?(project_name) then
    @cached_projects_by_name[project_name] = this_project
  else
    @logger.warn "  Warning caching Project by Name: #{project_name}. Duplicate name found!"
    @logger.warn "  Only first instance of this Project name will be cached."
  end

  return this_project, project_workspace_oid

end

#cache_refresh_neededObject

Determine whether or not to refresh local sub/workspace/project cache



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/rally_user_helper.rb', line 470

def cache_refresh_needed()

  refresh_needed = false
  reason = ""
  subscription_cache_filename = File.dirname(__FILE__) + "/" + SUB_CACHE
  workspace_cache_filename = File.dirname(__FILE__) + "/" + WORKSPACE_CACHE
  project_cache_filename = File.dirname(__FILE__) + "/" + PROJECT_CACHE

  if !FileTest.exist?(subscription_cache_filename) ||
     !FileTest.exist?(workspace_cache_filename) ||
     !FileTest.exist?(project_cache_filename) then
     refresh_needed = true
     reason = "One or more cache files is not found."
     return refresh_needed, reason
  end

  cache_age = get_cache_age()
  if cache_age > @max_cache_age then
    refresh_needed = true
    reason = "Age of workspace/project cache is greater than specified max of #{@max_cache_age}"
    return refresh_needed, reason
  end

  read_subscription_cache()
  cached_subscription_id = @cached_sub_id
  current_subscription_id = get_current_sub_id()

  if current_subscription_id != cached_subscription_id then
    refresh_needed = true
    reason = "Specified SubID: #{current_subscription_id} is different from cached SubID: #{cached_subscription_id}"
    return refresh_needed, reason
  end

  # If we've fallen through to here, no refresh is needed
  reason = "No workspace/project cache refresh currently required"
  return refresh_needed, reason
end

#cache_subscription_entry(header, row) ⇒ Object

Add row to subscription cache



514
515
516
517
518
519
520
521
522
523
524
# File 'lib/rally_user_helper.rb', line 514

def cache_subscription_entry(header, row)
  subscription_id                       = row[header[0]].strip
  subscription_name                     = row[header[1]].strip

  this_subscription                     = {}
  this_subscription["SubscriptionID"]   = subscription_id
  this_subscription["Name"]             = subscription_name

  @cached_sub_id                        = subscription_id.to_i
  @cached_subscription[subscription_id] = this_subscription
end

#cache_usersObject

added for performance



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rally_user_helper.rb', line 196

def cache_users()

  user_query = RallyAPI::RallyQuery.new()
  user_query.type = :user
  user_query.fetch = @initial_user_fetch
  user_query.page_size = 200 #optional - default is 200
  user_query.limit = 90000 #optional - default is 99999
  user_query.order = "UserName Asc"

    # Filter for enabled only
  if @summarize_enabled_only then
    user_query.query_string = @enabled_only_filter
    number_found_suffix = "Enabled Users."
  else
    number_found_suffix = "Users."
  end

  initial_query_results = @rally.find(user_query)

  number_users = initial_query_results.total_result_count
  count = 1
  notify_increment = 25
  @cached_users = {}
  initial_query_results.each do | initial_user |
    notify_remainder=count%notify_increment
    if notify_remainder==0 then @logger.info "Cached #{count} of #{number_users} #{number_found_suffix}" end

    # Follow-up user-by-user query of Rally for Detailed User Properties
    user_query.fetch = @detail_user_fetch

    # Setup query parameters for Rally query of detailed user info
    this_user_name = initial_user["UserName"]
    query_string = "(UserName = \"#{this_user_name}\")"
    user_query.query_string = query_string

    # Query Rally for single-user detailed info, including Permissions, Projects, and
    # Team Memberships
    detail_user_query_results = @rally.find(user_query)

    # If found, cache the user
    number_found = detail_user_query_results.total_result_count
    if number_found > 0 then
      this_user = detail_user_query_results.first
      @cached_users[this_user.UserName] = this_user
      count+=1
    else
      @logger.warn "User: #{this_user_name} not found in follow-up query. Skipping..."
      next
    end

  end
end

#cache_workspace_entry(header, row) ⇒ Object

Add row to workspace cache



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/rally_user_helper.rb', line 553

def cache_workspace_entry(header, row)
  workspace_id               = row[header[0]].strip
  workspace_name             = row[header[1]].strip
  workspace_state            = row[header[2]].strip

  this_workspace = {}
  this_workspace["ObjectID"] = workspace_id
  this_workspace["Name"]     = workspace_name
  this_workspace["State"]    = workspace_state
  this_workspace["_ref"]     = make_ref_from_oid("workspace", workspace_id)

  @cached_workspaces[workspace_id] = this_workspace
  if !@cached_workspaces_by_name.has_key?(workspace_name) then
    @cached_workspaces_by_name[workspace_name] = this_workspace
  else
    @logger.warn "  Warning caching Workspace by Name: #{workspace_name}. Duplicate name found!"
    @logger.warn "  Only first instance of this Workspace name will be cached."
  end
end

#cache_workspaces_projectsObject

Added for performance



793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/rally_user_helper.rb', line 793

def cache_workspaces_projects()
  @cached_subscription = {}
  @cached_workspaces = {}
  @cached_projects = {}
  @workspace_hash_of_projects = {}

  subscription_query = RallyAPI::RallyQuery.new()
  subscription_query.type = :subscription
  subscription_query.fetch = "Name,SubscriptionID,Workspaces,Name,State,ObjectID"
  subscription_query.page_size = 200 #optional - default is 200
  subscription_query.limit = 50000 #optional - default is 99999
  subscription_query.order = "Name Asc"

  results = @rally.find(subscription_query)

  # pre-populate workspace hash
  results.each do | this_subscription |

    this_subscription_id = this_subscription["SubscriptionID"]
    @cached_subscription[this_subscription_id] = this_subscription
    @cached_sub_id = this_subscription_id

    @logger.info "This subscription has: #{this_subscription.Workspaces.length} workspaces."

    workspaces = this_subscription.Workspaces
    workspaces.each do |this_workspace|

      this_workspace_oid_string = this_workspace["ObjectID"].to_s

      # Look for open projects within Workspace
      open_projects = get_open_projects(this_workspace)
      @workspace_hash_of_projects[this_workspace_oid_string] = open_projects

      if this_workspace.State != "Closed" && open_projects != nil then
        @logger.info "Caching Workspace:  #{this_workspace['Name']}."
        @cached_workspaces[this_workspace_oid_string] = this_workspace

        if !@cached_workspaces_by_name.has_key?(this_workspace["Name"]) then
          @cached_workspaces_by_name[this_workspace["Name"]]
        else
          @logger.warn "  Warning caching Workspace by Name: #{this_workspace["Name"]}. Duplicate name found!"
          @logger.warn "  Only first instance of this Workspace name will be cached."
        end

        @logger.info "Workspace: #{this_workspace['Name']} has: #{open_projects.length} open projects."

        # Loop through open projects and Cache
        open_projects.each do | this_project |
          this_project["WorkspaceOIDNumeric"] = this_workspace["ObjectID"]
          @cached_projects[this_project.ObjectID.to_s] = this_project

          if !@cached_projects_by_name.has_key?(this_project["Name"]) then
            @cached_projects_by_name[this_project["Name"]]
          else
            @logger.warn "  Warning caching Project by Name: #{this_project["Name"]}. Duplicate name found!"
            @logger.warn "  Only first instance of this Project name will be cached."
          end
        end
      else
          @logger.warn "Workspace:  #{this_workspace['Name']} is closed or has no open projects. Not added to cache."
      end
    end
  end

  write_subscription_cache()
  write_workspace_cache()
  write_project_cache()
end

#calc_file_age(filename) ⇒ Object

Given the name of a file, calculates age of that file



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/rally_user_helper.rb', line 423

def calc_file_age(filename)
  today = Time.now

  # Return really big number to prompt refresh if files not found
  file_age = 10000

  if !FileTest.exist?(filename) then
    # Maintain "really big" age and return
    return file_age
  end

  file_reference = File.new(filename, "r")
  # Round fractional days up
  file_age = ((today - file_reference.mtime)/86400).ceil
  return file_age
end

#check_default_permission_type(value) ⇒ Object

Checks to see if input record is using: Text string (i.e. Editor, Viewer) Or User (i.e. [email protected]) as source of default permissions



1233
1234
1235
1236
1237
1238
1239
1240
# File 'lib/rally_user_helper.rb', line 1233

def check_default_permission_type(value)

    type = :stringsource
    if !value.match(/@/).nil? then
        type = :usersource
    end
    return type
end

#create_user(user_name, fields = user_fields) ⇒ Object



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
# File 'lib/rally_user_helper.rb', line 1133

def create_user(user_name, fields = user_fields)

  new_user_obj = {}

  new_user_obj["UserName"] = user_name.downcase
  new_user_obj["EmailAddress"] = user_name.downcase

  fields.each_pair do | key, value |
    new_user_obj[key] = value
  end

  new_user = nil

  begin
    if @create_flag
      new_user = @rally.create(:user, new_user_obj)
    end
    @logger.info "Created Rally user #{user_name.downcase}"
  rescue
    @logger.error "Error creating user: #{$!}"
    raise $!
  end

  # Grab full object of the created user and return so that we can use it later
  new_user_query = RallyAPI::RallyQuery.new()
  new_user_query.type = :user
  new_user_query.fetch = "UserName,FirstName,LastName,DisplayName,UserPermissions,Name,Role,Workspace,ObjectID,Project,ObjectID,TeamMemberships"
  new_user_query.query_string = "(UserName = \"#{user_name.downcase}\")"
  new_user_query.order = "UserName Asc"

  query_results = @rally.find(new_user_query)
  new_user_created = query_results.first

  # Cache the new user
  @cached_users[user_name.downcase] = new_user_created
  return new_user_created
end

#create_workspace_permission(user, workspace, permission) ⇒ Object

Create Admin, User, or Viewer permissions for a Workspace



1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
# File 'lib/rally_user_helper.rb', line 1330

def create_workspace_permission(user, workspace, permission)
  # Keep backward compatibility of our old permission names
  if permission == VIEWER || permission == EDITOR
    permission = USER
  end

  if permission != NOACCESS
    new_permission_obj = {}
    new_permission_obj["Workspace"] = workspace["_ref"]
    new_permission_obj["User"] = user._ref
    new_permission_obj["Role"] = permission

    if @create_flag then new_permission = @rally.create(:workspacepermission, new_permission_obj) end
  end
end

#disable_user(user) ⇒ Object



1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'lib/rally_user_helper.rb', line 1199

def disable_user(user)
  if user.Disabled == 'False'
    if @create_flag
      fields = {}
      fields["Disabled"] = 'False'
      updated_user = @rally.update(:user, user._ref, fields) #by ref
    end

    @logger.info "#{user["UserName"]} disabled in Rally"
  else
    @logger.info "#{user["UserName"]} already disabled from Rally"
    return false
  end
  return true
end

#does_user_have_project_permission?(project, user) ⇒ Boolean

Check to see if User has any permission within a specific project

Returns:

  • (Boolean)


1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
# File 'lib/rally_user_helper.rb', line 1400

def does_user_have_project_permission?(project, user)

  # set default return value
  project_permission_exists = false

  use_cache = false

  # first try to lookup against cached user list -- much faster than re-querying Rally
  if @cached_users != nil then
    if @cached_users.has_key?(user.UserName) then use_cache = true end
  end

  # first try to lookup against cached user list -- much faster than re-querying Rally
  if use_cache then

    this_user = @cached_users[user.UserName]

    # loop through permissions and look to see if there's an existing permission for this
    # workspace, and if so, has it changed

    user_permissions = this_user.UserPermissions

    user_permissions.each do |this_permission|

      if this_permission._type == "ProjectPermission" then
        # user has existing permissions in this project - let's compare new role against existing
        if this_permission.Project.ObjectID.to_s == project["ObjectID"].to_s then
          project_permission_exists = true
          break
        end
      end
    end

  else # Cache does not exist or user isn't in it - query info from Rally

    project_permission_query = RallyAPI::RallyQuery.new()
    project_permission_query.type = :projectpermission
    project_permission_query.fetch = "Project,Name,ObjectID,Role,User"
    project_permission_query.page_size = 200 #optional - default is 200
    project_permission_query.order = "Name Asc"
    project_permission_query.query_string = "(User.UserName = \"" + user.UserName + "\")"

    query_results = @rally.find(project_permission_query)

    project_permission_exists = false

    # Look to see if any existing ProjectPermissions for this user match the one we're examining
    # If so, check to see if the project permissions are any different
    query_results.each { |pp|

      if ( pp.Project.ObjectID == project["ObjectID"])
        project_permission_exists = true
        break
      end
    }
  end
  return project_permission_exists
end

#does_user_have_workspace_permission?(workspace, user) ⇒ Boolean

Check to see if User has any permission within a specific workspace

Returns:

  • (Boolean)


1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
# File 'lib/rally_user_helper.rb', line 1347

def does_user_have_workspace_permission?(workspace, user)
  # set default return value
  workspace_permission_exists = false

  use_cache = false

  # first try to lookup against cached user list -- much faster than re-querying Rally
  if @cached_users != nil then
    if @cached_users.has_key?(user.UserName) then use_cache = true end
  end

  # first try to lookup against cached user list -- much faster than re-querying Rally
  if use_cache then

    this_user = @cached_users[user.UserName]

    # loop through permissions and look to see if there's an existing permission for this
    # workspace, and if so, has it changed
    user_permissions = this_user.UserPermissions
    user_permissions.each do | this_permission |
      if this_permission._type == "WorkspacePermission" then
        if this_permission.Workspace.ObjectID.to_s == workspace["ObjectID"].to_s then
          workspace_permission_exists = true
          break
        end
      end
    end

  else # Cache does not exist or user isn't in it - query info from Rally
    workspace_permission_query = RallyAPI::RallyQuery.new()
    workspace_permission_query.type = :workspacepermission
    workspace_permission_query.fetch = "Workspace,Name,ObjectID,Role,User"
    workspace_permission_query.page_size = 200 #optional - default is 200
    workspace_permission_query.order = "Name Asc"
    workspace_permission_query.query_string = "(User.UserName = \"" + user.UserName + "\")"

    query_results = @rally.find(workspace_permission_query)

    workspace_permission_exists = false

    # Look to see if any existing WorkspacePermissions for this user match the one we're examining
    # If so, check to see if the workspace permissions are any different
    query_results.each { |wp|
      if ( wp.Workspace.ObjectID == workspace["ObjectID"])
        workspace_permission_exists = true
        break
      end
    }
  end
  return workspace_permission_exists
end

#enable_user(user) ⇒ Object



1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
# File 'lib/rally_user_helper.rb', line 1215

def enable_user(user)
  if user.Disabled == 'True'
    fields = {}
    fields["Disabled"] = 'True'
    updated_user = @rally.update(:user, user._ref, fields) if @create_flag
    @logger.info "#{user["UserName"]} enabled in Rally"
    return true
  else
    @logger.info "#{user["UserName"]} already enabled in Rally"
    return false
  end
end

#find_project(object_id) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rally_user_helper.rb', line 357

def find_project(object_id)
  if @cached_projects.has_key?(object_id)
    # Found project in cache, return the cached project
    return @cached_projects[object_id]
  else
    # project not found in cache - go to Rally
    project_query                    = RallyAPI::RallyQuery.new()
    project_query.type               = :project
    project_query.fetch              = "Name,State,ObjectID,Workspace,ObjectID"
    project_query.query_string       = "((ObjectID = \"#{object_id}\") AND (State = \"Open\"))"

    project_results                  = @rally.find(project_query)

    if project_results.total_result_count != 0 then
      # Project found via Rally query, return it
      project = project_results.first()

      # Cache it for use next time
      @cached_projects[project["ObjectID"]] = project
      @logger.info "Caching Project: #{project['Name']}"

      # Return it
      return project
    else
      # Project not found in Rally _or_ cache - return Nil
      @logger.warn "Rally Project: #{object_id} not found"
      return nil
    end
  end
end

#find_project_by_name(project_name) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rally_user_helper.rb', line 319

def find_project_by_name(project_name)
  if @cached_projects_by_name.has_key?(project_name)
    # Found workspace in cache, return the cached workspace
    return @cached_projects_by_name[project_name]
  else
    # workspace not found in cache - go to Rally
    project_query                    = RallyAPI::RallyQuery.new()
    project_query.project            = nil
    project_query.type               = :workspace
    project_query.fetch              = "Name,State,ObjectID"
    project_query.query_string       = "((Name = \"#{project_name}\") AND (State = \"Open\"))"

    project_results                  = @rally.find(project_query)

    if project_results.total_result_count != 0 then
      # Workspace found via Rally query, return it
      project = project_results.first()

      # Cache it for use next time
      @cached_projects_by_name[project["Name"]] = project
      @logger.info "Caching Project: #{project['Name']}"

      if project_results.total_result_count > 1 then
        # More than one Project matching this name found
        @logger.warn "More than one Project of name: #{project_name} found."
        @logger.warn "Returning only the first instance found!"
      end

      # Return project object
      return project
    else
      # Project not found in Rally _or_ cache - return Nil
      @logger.warn "Rally Project: #{project_name} not found"
      return nil
    end
  end
end

#find_user(name) ⇒ Object

Helper methods Does the user exist? If so, return the user, if not return nil Need to downcase the name since user names are downcased when created. Without downcase, we would not be

able to find '[email protected]'


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rally_user_helper.rb', line 120

def find_user(name)
  if ( name.downcase != name )
    @logger.info "Looking for #{name.downcase} instead of #{name}"
  end

  if @cached_users.has_key?(name.downcase)
    return @cached_users[name.downcase]
  end

  single_user_query = RallyAPI::RallyQuery.new()
  single_user_query.type = :user
  single_user_query.fetch = @detail_user_fetch
  single_user_query.page_size = 200 #optional - default is 200
  single_user_query.limit = 90000 #optional - default is 99999
  single_user_query.order = "UserName Asc"
  single_user_query.query_string = "(UserName = \"" + name + "\")"

  query_results = @rally.find(single_user_query)

  if query_results.total_result_count == 0
    return nil
  else
    # Cache user for use next time
    this_user = query_results.first
    @cached_users[this_user["UserName"].downcase] = this_user
    @logger.info "Caching User: #{this_user.UserName}"

    return this_user
  end
end

#find_workspace(object_id) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rally_user_helper.rb', line 249

def find_workspace(object_id)
  if @cached_workspaces.has_key?(object_id)
    # Found workspace in cache, return the cached workspace
    return @cached_workspaces[object_id]
  else
    # workspace not found in cache - go to Rally
    workspace_query                    = RallyAPI::RallyQuery.new()
    workspace_query.project            = nil
    workspace_query.type               = :workspace
    workspace_query.fetch              = "Name,State,ObjectID"
    workspace_query.query_string       = "((ObjectID = \"#{object_id}\") AND (State = \"Open\"))"

    workspace_results                  = @rally.find(workspace_query)

    if workspace_results.total_result_count != 0 then
      # Workspace found via Rally query, return it
      workspace = workspace_results.first()

      # Cache it for use next time
      @cached_workspaces[workspace["ObjectID"]] = workspace
      @logger.info "Caching Workspace: #{workspace['Name']}"

      # Return workspace object
      return workspace
    else
      # Workspace not found in Rally _or_ cache - return Nil
      @logger.warn "Rally Workspace: #{object_id} not found"
      return nil
    end
  end
end

#find_workspace_by_name(workspace_name) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rally_user_helper.rb', line 281

def find_workspace_by_name(workspace_name)
  if @cached_workspaces_by_name.has_key?(workspace_name)
    # Found workspace in cache, return the cached workspace
    return @cached_workspaces_by_name[workspace_name]
  else
    # workspace not found in cache - go to Rally
    workspace_query                    = RallyAPI::RallyQuery.new()
    workspace_query.project            = nil
    workspace_query.type               = :workspace
    workspace_query.fetch              = "Name,State,ObjectID"
    workspace_query.query_string       = "((Name = \"#{workspace_name}\") AND (State = \"Open\"))"

    workspace_results                  = @rally.find(workspace_query)

    if workspace_results.total_result_count != 0 then
      # Workspace found via Rally query, return it
      workspace = workspace_results.first()

      # Cache it for use next time
      @cached_workspaces_by_name[workspace["Name"]] = workspace
      @logger.info "Caching Workspace: #{workspace['Name']}"

      if workspace_results.total_result_count > 1 then
        # More than one Workspace matching this name found
        @logger.warn "More than one Workspace of name: #{workspace_name} found."
        @logger.warn "Returning only the first instance found!"
      end

      # Return workspace object
      return workspace
    else
      # Workspace not found in Rally _or_ cache - return Nil
      @logger.warn "Rally Workspace: #{workspace_name} not found"
      return nil
    end
  end
end

#get_cache_ageObject

Determine cache age



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/rally_user_helper.rb', line 441

def get_cache_age()

  subscription_cache_filename = File.dirname(__FILE__) + "/" + SUB_CACHE
  workspace_cache_filename = File.dirname(__FILE__) + "/" + WORKSPACE_CACHE
  project_cache_filename = File.dirname(__FILE__) + "/" + PROJECT_CACHE

  # Default to really big number to prompt refresh if files not found
  cache_age = 10000

  if !FileTest.exist?(subscription_cache_filename) ||
    !FileTest.exist?(workspace_cache_filename) ||
    !FileTest.exist?(project_cache_filename) then
      # Maintain "really big" age and return
      return cache_age
  end

  age_array = [
    calc_file_age(subscription_cache_filename),
    calc_file_age(workspace_cache_filename),
    calc_file_age(project_cache_filename)
  ]
  min_age = age_array.min

  # return the age of the youngest cache file (should all be the same though)
  cache_age = min_age
  return cache_age
end

#get_cached_projectsObject



100
101
102
# File 'lib/rally_user_helper.rb', line 100

def get_cached_projects()
  return @cached_projects
end

#get_cached_projects_by_nameObject



112
113
114
# File 'lib/rally_user_helper.rb', line 112

def get_cached_projects_by_name()
  return @cached_projects_by_name
end

#get_cached_usersObject



92
93
94
# File 'lib/rally_user_helper.rb', line 92

def get_cached_users()
  return @cached_users
end

#get_cached_workspacesObject



96
97
98
# File 'lib/rally_user_helper.rb', line 96

def get_cached_workspaces()
  return @cached_workspaces
end

#get_cached_workspaces_by_naneObject



108
109
110
# File 'lib/rally_user_helper.rb', line 108

def get_cached_workspaces_by_nane()
  return @cached_workspaces_by_name
end

#get_current_sub_idObject

Get current SubID



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/rally_user_helper.rb', line 405

def get_current_sub_id()

  subscription_query = RallyAPI::RallyQuery.new()
  subscription_query.type = :subscription
  subscription_query.fetch = "Name,SubscriptionID"
  subscription_query.page_size = 200 #optional - default is 200
  subscription_query.limit = 50000 #optional - default is 99999
  subscription_query.order = "Name Asc"

  results = @rally.find(subscription_query)

  this_subscription = results.first
  this_sub_id = this_subscription["SubscriptionID"]

  return this_sub_id
end

#get_membership_oid_from_membership(team_membership) ⇒ Object

Pulls Project OID off of team membership _ref



1243
1244
1245
1246
1247
# File 'lib/rally_user_helper.rb', line 1243

def get_membership_oid_from_membership(team_membership)
  this_membership_ref = team_membership._ref
  this_membership_oid = this_membership_ref.split("\/")[-1].split("\.")[0]
  return this_membership_oid
end

#get_open_projects(input_workspace) ⇒ Object

Get a list of OPEN projects in Workspace ========================


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rally_user_helper.rb', line 177

def get_open_projects (input_workspace)
  project_query                          = RallyAPI::RallyQuery.new()
  project_query.workspace                = input_workspace
  project_query.project                  = nil
  project_query.project_scope_up         = true
  project_query.project_scope_down       = true
  project_query.type                     = :project
  project_query.fetch                    = "Name,State,ObjectID,Workspace,ObjectID"
  project_query.query_string             = "(State = \"Open\")"

  begin
    open_projects     = @rally.find(project_query)
  rescue Exception => ex
    open_projects = nil
  end
  return (open_projects)
end

#get_workspace_project_hashObject



104
105
106
# File 'lib/rally_user_helper.rb', line 104

def get_workspace_project_hash()
  return @workspace_hash_of_projects
end

#is_project_in_workspace(project, workspace) ⇒ Object

Check to see if a project is within a particular workspace.



389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/rally_user_helper.rb', line 389

def is_project_in_workspace(project, workspace)
  test_project_oid = project["ObjectID"]
  this_workspace_oid = workspace["ObjectID"]

  object_id_matches = false
  these_projects = @workspace_hash_of_projects[this_workspace_oid]
  these_projects.each do | this_project |
    this_project_oid = this_project["ObjectID"]
    if test_project_oid == this_project_oid then
      object_id_matches = true
    end
  end
  return object_id_matches
end

#is_team_member(project_oid, user) ⇒ Object

Checks to see if a user is a Team Member for a particular project



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
# File 'lib/rally_user_helper.rb', line 1250

def is_team_member(project_oid, user)

    # Default values
    is_member = false
    return_value = "No"

    team_memberships = user["TeamMemberships"]

    # First check if team_memberships are nil then loop through and look for a match on
    # Project OID
    if team_memberships != nil then

      team_memberships.each do |this_membership|

        # Grab the Project OID off of the ref URL
        this_membership_oid = get_membership_oid_from_membership(this_membership)
        if this_membership_oid == project_oid then
          is_member = true
        end
      end
    end

    if is_member then return_value = "Yes" end
    return return_value
end

#make_ref_from_oid(object_type, object_id) ⇒ Object

Create ref from oid



509
510
511
# File 'lib/rally_user_helper.rb', line 509

def make_ref_from_oid(object_type, object_id)
  return "/#{object_type}/#{object_id}"
end

#read_project_cacheObject

Read project cache



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/rally_user_helper.rb', line 630

def read_project_cache()

  project_cache_filename = File.dirname(__FILE__) + "/" + PROJECT_CACHE
  @logger.info "Started reading project cache from #{project_cache_filename}"

  # Read in project cache items (should be only 1)
  project_cache_input  = CSV.read(project_cache_filename, {:col_sep => CACHE_COL_DELIM, :encoding => @file_encoding})

  header = project_cache_input.first #ignores first line

  rows   = []
  (1...project_cache_input.size).each { |i| rows << CSV::Row.new(header, project_cache_input[i]) }

  number_processed = 0

  current_workspace_oid_string = "-9999"
  these_projects = []

  rows.each do |row|
    if !row.nil? then
      this_project, this_workspace_oid = cache_project_entry(header, row)

      # make sure workspace OID is a string
      this_workspace_oid_string = this_workspace_oid.to_s

      if @workspace_hash_of_projects.has_key?(this_workspace_oid_string) then
        these_projects = @workspace_hash_of_projects[this_workspace_oid_string]
      else
        these_projects = []
      end

      these_projects.push(this_project)
      @workspace_hash_of_projects[this_workspace_oid_string] = these_projects

      number_processed += 1
    end

  end
  @logger.info "Completed reading project cache from #{project_cache_filename}"
  @logger.info "Read and cached a total of #{number_processed} projects from local cache file."
end

#read_subscription_cacheObject

Read subscription cache



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/rally_user_helper.rb', line 527

def read_subscription_cache()

  subscription_cache_filename = File.dirname(__FILE__) + "/" + SUB_CACHE
  @logger.info "Started reading subscription cache from #{subscription_cache_filename}"

  # Read in Subscription cache items (should be only 1)
  subscription_cache_input  = CSV.read(subscription_cache_filename, {:col_sep => CACHE_COL_DELIM, :encoding => @file_encoding})

  header = subscription_cache_input.first #ignores first line

  rows   = []
  (1...subscription_cache_input.size).each { |i| rows << CSV::Row.new(header, subscription_cache_input[i]) }

  number_processed = 0

  rows.each do |row|
    if !row.nil? then
      cache_subscription_entry(header, row)
      number_processed += 1
    end
  end

  @logger.info "Completed reading subscription cache from #{subscription_cache_filename}"
end

#read_workspace_cacheObject

Read workspace cache



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/rally_user_helper.rb', line 574

def read_workspace_cache()

  workspace_cache_filename = File.dirname(__FILE__) + "/" + WORKSPACE_CACHE
  @logger.info "Started reading workspace cache from #{workspace_cache_filename}"

  # Read in workspace cache items (should be only 1)
  workspace_cache_input  = CSV.read(workspace_cache_filename, {:col_sep => CACHE_COL_DELIM, :encoding => @file_encoding})

  header = workspace_cache_input.first #ignores first line

  rows   = []
  (1...workspace_cache_input.size).each { |i| rows << CSV::Row.new(header, workspace_cache_input[i]) }

  number_processed = 0

  rows.each do |row|
    if !row.nil? then
      cache_workspace_entry(header, row)
      number_processed += 1
    end
  end
  @logger.info "Completed reading workspace cache from #{workspace_cache_filename}"
  @logger.info "Read and cached a total of #{number_processed} workspaces from local cache file."
end

#read_workspace_project_cacheObject



778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/rally_user_helper.rb', line 778

def read_workspace_project_cache()
  # Caches by OID
  @cached_subscription = {}
  @cached_workspaces = {}
  @cached_projects = {}

  @cached_workspaces_by_name = {}
  @cached_projects_by_name = {}

  read_subscription_cache()
  read_workspace_cache()
  read_project_cache()
end

#refresh_user(name) ⇒ Object

Needed to refresh user object after updates, since user cache is stale after user settings are changed by the helper



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rally_user_helper.rb', line 153

def refresh_user(name)
  single_user_query = RallyAPI::RallyQuery.new()
  single_user_query.type = :user
  single_user_query.fetch = @detail_user_fetch
  single_user_query.page_size = 200 #optional - default is 200
  single_user_query.limit = 90000 #optional - default is 99999
  single_user_query.order = "UserName Asc"
  single_user_query.query_string = "(UserName = \"" + name + "\")"

  query_results = @rally.find(single_user_query)
  if query_results.total_result_count == 0
    return nil
  else
    # Cache user for use next time
    this_user = query_results.first
    @cached_users[this_user["UserName"].downcase] = this_user
    @logger.info "Refreshed User: #{this_user.UserName}"

    return this_user
  end
end

#sync_project_permissions(source_user_id, target_user_id) ⇒ Object

Mirrors ProjectPermission set from a source user to a target user Note that creation of a ProjectPermission will automatically create the minimum needed WorkspacePermission for the “owning” Workspace, if a WorkspacePermission does not already exist. Thus there’s no need for a sync_workspace_permissions method



867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
# File 'lib/rally_user_helper.rb', line 867

def sync_project_permissions(source_user_id, target_user_id)
  source_user = find_user(source_user_id)
  target_user = find_user(target_user_id)
  if source_user.nil? then
    @logger.warn "  Source user: #{source_user_id} Not found. Skipping sync of permissions to #{target_user_id}."
    return
  elsif target_user.nil then
    @logger.warn "  Target user: #{target_user_id} Not found. Skipping sync of permissions for #{target_user_id}."
  end

  permissions_existing = target_user.UserPermissions
  source_permissions = source_user.UserPermissions

  # build permission hashes by Project ObjectID
  source_permissions_by_project = {}
  source_permissions.each do | this_source_permission |
    if this_source_permission._type == "ProjectPermission" then
      source_permissions_by_project[this_source_permission.Project.ObjectID.to_s] = this_source_permission
    end
  end

  permissions_existing_by_project = {}
  permissions_existing.each do | this_permission |
    if this_permission._type == "ProjectPermission" then
      permissions_existing_by_project[this_permission.Project.ObjectID.to_s] = this_permission
    end
  end

  # Prepare arrays of permissions to update, create, or delete
  permissions_to_update = []
  permissions_to_create = []
  permissions_to_delete = []

  # Check target permissions list for permissions to create and/or update
  source_permissions_by_project.each_pair do | this_source_project_oid, this_source_permission |

    # If target hash doesn't contain the OID referenced in the source permission set, it's a new
    # permission we need to create
    if !permissions_existing_by_project.has_key?(this_source_project_oid) then
      permissions_to_create.push(this_source_permission)

    # We found the OID key, so there is an existing permission for this Project. Is it different
    # from the target permission?
    else
      this_source_role = this_source_permission.Role
      this_source_project = find_project(this_source_project_oid)
      this_source_project_name = this_source_project["Name"]

      if project_permissions_different?(this_source_project, target_user, this_source_role) then
        existing_permission = permissions_existing_by_project[this_source_project_oid]
        this_existing_project = existing_permission.Project
        this_existing_project_name = this_existing_project["Name"]
        this_existing_role = existing_permission.Role
        @logger.info "Existing Permission: #{this_existing_project_name}: #{this_existing_role}"
        @logger.info "Updated Permission: #{this_source_project_name}: #{this_source_role}"
        permissions_to_update.push(this_source_permission)
      end
    end
  end

  # Loop through target permissions list and check for Project Permissions that don't exist
  # in source permissions template, indicating they need to be removed
  permissions_existing_by_project.each_pair do | this_existing_project_oid, this_existing_permission |
    if !source_permissions_by_project.has_key?(this_existing_project_oid) then
      permissions_to_delete.push(this_existing_permission)
    end
  end

  # Process creates
  number_new_permissions = 0
  permissions_to_create.each do | this_new_permission |
    this_project = find_project(this_new_permission.Project.ObjectID.to_s)
    if !this_project.nil? then

      this_project = this_new_permission.Project
      this_workspace = this_project.Workspace
      this_workspace.read

      this_project_name = this_new_permission.Project.Name
      this_workspace_name = this_workspace.Name

      this_role = this_new_permission.Role

      @logger.info "Workspace: #{this_workspace_name}"
      @logger.info "Creating #{this_role} permission on #{this_project_name} from #{source_user_id} to: #{target_user_id}."
      create_project_permission(target_user, this_project, this_role)
      number_new_permissions += 1
    end
  end

  # Process updates
  number_updated_permissions = 0
  permissions_to_update.each do | this_new_permission |
    this_project = find_project(this_new_permission.Project.ObjectID.to_s)
    if !this_project.nil? then
      this_project = this_new_permission.Project
      this_workspace = this_project.Workspace
      this_workspace.read

      this_project_name = this_new_permission.Project.Name
      this_workspace_name = this_workspace.Name

      this_role = this_new_permission.Role

      @logger.info "Workspace: #{this_workspace_name}"
      @logger.info "Updating #{this_role} permission on #{this_project_name} from #{source_user_id} to: #{target_user_id}."
      create_project_permission(target_user, this_project, this_role)
      number_updated_permissions += 1
    end
  end

  # Process deletes
  number_removed_permissions = 0
  permissions_to_delete.each do | this_deleted_permission |
    this_project = find_project(this_deleted_permission.Project.ObjectID.to_s)
    if !this_project.nil? then
      this_project = this_deleted_permission.Project
      this_workspace = this_project.Workspace
      this_workspace.read

      this_project_name = this_deleted_permission.Project.Name
      this_workspace_name = this_workspace.Name

      this_role = this_deleted_permission.Role

      @logger.info "Workspace: #{this_workspace_name}"
      @logger.info "Removing #{this_role} permission to #{this_project_name} from #{target_user_id} since it is not present on source: #{source_user_id}."
      if !@upgrade_only_mode then
        delete_project_permission(target_user, this_project)
        number_removed_permissions += 1
      else
        @logger.info "  #{target_user_id} - upgrade_only_mode == true."
        @logger.info "  Proposed Permission removal would downgrade permissions. No permission removal applied."
      end
    end
  end
  @logger.info "#{number_new_permissions} Permissions Created; #{number_updated_permissions} Permissions Updated; #{number_removed_permissions} Permissions Removed."
end

#sync_team_memberships(source_user_id, target_user_id) ⇒ Object

Mirrors team membership set from a source user to a target user



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/rally_user_helper.rb', line 1007

def sync_team_memberships(source_user_id, target_user_id)
  source_user = find_user(source_user_id)
  target_user = find_user(target_user_id)
  if source_user.nil? then
    @logger.warn "  Source user: #{source_user_id} Not found. Skipping sync of permissions to #{target_user_id}."
    return
  elsif target_user.nil then
    @logger.warn "  Target user: #{target_user_id} Not found. Skipping sync of permissions for #{target_user_id}."
  end

  memberships_existing = target_user["TeamMemberships"]
  source_memberships = source_user["TeamMemberships"]

  # build membership lists by Project ObjectID
  source_membership_oids = []
  source_memberships.each do | this_source_membership |
    source_membership_oids.push(get_membership_oid_from_membership(this_source_membership))
  end

  memberships_existing_oids = []
  memberships_existing.each do | this_membership |
    memberships_existing_oids.push(get_membership_oid_from_membership(this_membership))
  end

  # build Target User Permissions list by Project ObjectID
  # Needed to make sure that user to whom we're trying to set team membership for
  # is an editor
  permissions_existing = target_user.UserPermissions
  permissions_existing_by_project = {}
  permissions_existing.each do | this_permission |
    if this_permission._type == "ProjectPermission" then
      permissions_existing_by_project[this_permission.Project.ObjectID.to_s] = this_permission
    end
  end

  memberships_to_add = []
  source_membership_oids.each do | this_membership_oid |
    if !memberships_existing_oids.include?(this_membership_oid) then
      memberships_to_add.push(this_membership_oid)
    end
  end

  memberships_to_remove = []
  memberships_existing_oids.each do | this_membership_oid |
    if !source_membership_oids.include?(this_membership_oid) then
      memberships_to_remove.push(this_membership_oid)
    end
  end

  number_memberships_added = 0
  memberships_to_add.each do | this_membership_oid |

    this_permission = permissions_existing_by_project[this_membership_oid]
    if !this_permission.nil? then
      this_role = this_permission.Role
      if !this_role.eql?(EDITOR) && !this_role.eql(PROJECTADMIN) then
        @logger.warn "  Target User: #{target_user_id} must be an Editor or Project Admin to make them a Team Member. Skipping."
        return
      end

      this_project = find_project(this_membership_oid)
      if !this_project.nil? then
        number_memberships_added += 1
        this_project_name = this_project["Name"]
        @logger.info "Updating TeamMembership on #{this_project_name} from #{source_user_id} to: #{target_user_id}."
        update_team_membership(target_user, this_membership_oid, this_project_name, TEAMMEMBER_YES)
      end
    end

  end

  number_memberships_removed = 0
  memberships_to_remove.each do | this_membership_oid |
    this_project = find_project(this_membership_oid)
    if !this_project.nil? then
      number_memberships_removed += 1
      this_project_name = this_project["Name"]
      @logger.info "Removing TeamMembership on #{this_project_name} from #{target_user_id} since source: #{source_user_id} is not a TeamMember."
      update_team_membership(target_user, this_membership_oid, this_project_name, TEAMMEMBER_NO)
    end
  end

  @logger.info "Team Memberships Added: #{number_memberships_added}; Team Memberships Removed: #{number_memberships_removed}"

end

#update_project_permissions(project, user, permission, new_user) ⇒ Object



1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/rally_user_helper.rb', line 1113

def update_project_permissions(project, user, permission, new_user)
  if new_user or project_permissions_different?(project, user, permission)
    if @upgrade_only_mode then
      is_upgrade, existing_permission = is_project_permission_upgrade?(project, user, permission)
      if is_upgrade then
        update_permission_projectlevel(project, user, permission)
      else
        @logger.warn "  #{user["UserName"]} #{project["Name"]} - upgrade_only_mode == true."
        @logger.warn "  Existing Permission: #{existing_permission}"
        @logger.warn "  Proposed Permission: #{permission}"
        @logger.warn "  Proposed Permission change would downgrade permissions. No permission updates applied."
      end
    else
      update_permission_projectlevel(project, user, permission)
    end
  else
    @logger.info "  #{user["UserName"]} #{project["Name"]} - Existing/new permission same. No permission updates applied."
  end
end

#update_team_membership(user, project_oid, project_name, team_member_setting) ⇒ Object

Updates team membership. Note - this utilizes un-documented and un-supported Rally endpoint that is not part of WSAPI REST it also digs down into rally_api to directly PUT against this endpoint not guaranteed to work forever



1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
# File 'lib/rally_user_helper.rb', line 1281

def update_team_membership(user, project_oid, project_name, team_member_setting)

  # look up user
  these_team_memberships = user["TeamMemberships"]
  this_user_oid = user["ObjectID"]

  # Default for whether user is member or not
  is_member = is_team_member(project_oid, user)

  url_base = make_team_member_url(this_user_oid, project_oid)

  # if User isn't a team member and update value is Yes then make them one
  if is_member == "No" && team_member_setting.downcase == TEAMMEMBER_YES.downcase then

    # Construct payload object
    my_payload = {}
    my_team_member_setting = {}
    my_team_member_setting ["TeamMember"] = "true"
    my_payload["projectuser"] = my_team_member_setting

    args = {:method => :put}
    args[:payload] = my_payload

    # @rally_json_connection does a to_json on object to convert
    # payload object to JSON: {"projectuser":{"TeamMember":"true"}}
    response = @rally_json_connection.send_request(url_base, args)
    @logger.info "  #{user["UserName"]} #{project_name} - Team Membership set to #{team_member_setting}"

    # if User is a team member and update value is No then remove them from team
  elsif is_member == "Yes" && team_member_setting.downcase == TEAMMEMBER_NO.downcase then

    # Construct payload object
    my_payload = {}
    my_team_member_setting = {}
    my_team_member_setting ["TeamMember"] = "false"
    my_payload["projectuser"] = my_team_member_setting

    args = {:method => :put}
    args[:payload] = my_payload

    # @rally_json_connection will convert payload object to JSON: {"projectuser":{"TeamMember":"false"}}
    response = @rally_json_connection.send_request(url_base, args)
    @logger.info "  #{user["UserName"]} #{project_name} - Team Membership set to #{team_member_setting}"
  else
    @logger.info "  #{user["UserName"]} #{project_name} - No creation of or changes to Team Membership"
  end
end

#update_user(user, fields = user_fields) ⇒ Object



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
# File 'lib/rally_user_helper.rb', line 1171

def update_user(user, fields = user_fields)

  user_name = user["UserName"]
  begin
    if @create_flag
      updated_user = user.update(fields)
    end
    @logger.info "Updated Rally user #{user_name.downcase}"
  rescue
    @logger.error "Error creating user: #{$!}"
    raise $!
  end

  # Grab full object of the created user and return so that we can use it later
  updated_user_query = RallyAPI::RallyQuery.new()
  updated_user_query.type = :user
  updated_user_query.fetch = "UserName,FirstName,LastName,DisplayName,UserPermissions,Name,Role,Workspace,ObjectID,Project,ObjectID,TeamMemberships"
  updated_user_query.query_string = "(UserName = \"#{user_name.downcase}\")"
  updated_user_query.order = "UserName Asc"

  query_results = @rally.find(updated_user_query)
  updated_user_object = query_results.first

  # Cache the new user
  @cached_users[user_name.downcase] = updated_user_object
  return
end

#update_workspace_permissions(workspace, user, permission, new_user) ⇒ Object



1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
# File 'lib/rally_user_helper.rb', line 1093

def update_workspace_permissions(workspace, user, permission, new_user)
  if new_user or workspace_permissions_different?(workspace, user, permission)
    if @upgrade_only_mode then
      is_upgrade, existing_permission = is_workspace_permission_upgrade?(workspace, user, permission)
      if is_upgrade then
        update_permission_workspacelevel(workspace, user, permission)
      else
        @logger.info "  #{user["UserName"]} #{workspace["Name"]} - upgrade_only_mode == true."
        @logger.warn "  Existing Permission: #{existing_permission}"
        @logger.warn "  Proposed Permission: #{permission}"
        @logger.info "  Proposed Permission change would downgrade permissions. No permission updates applied."
      end
    else
      update_permission_workspacelevel(workspace, user, permission)
    end
  else
    @logger.info "  #{user["UserName"]} #{workspace["Name"]} - Existing/new permission same. No permission updates applied."
  end
end

#write_project_cacheObject



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/rally_user_helper.rb', line 735

def write_project_cache()
  project_cache_filename = File.dirname(__FILE__) + "/" + PROJECT_CACHE
  @logger.info "Started writing project cache to #{project_cache_filename}"

  # The following results in an array of two-element arrays. The first element of each 2-element array
  # is the ProjectOID, or the key for the project hash. The second element is the value part of the hash
  projects_sorted_by_workspace = @cached_projects.sort_by {|key, value| value["WorkspaceOIDNumeric"]}

  # Output CSV header
  project_csv = CSV.open(
    project_cache_filename,
    CACHE_WRITE_MODE,
    {:col_sep => CACHE_COL_DELIM, :encoding => @file_encoding}
  )
  project_csv << PROJECT_CACHE_FIELDS

  # Output cache to file
  # Record for CSV output
  projects_sorted_by_workspace.each do | project_element |

    this_project = project_element[1]

    data = []

    project_id = this_project["ObjectID"]
    project_name = this_project["Name"]
    project_state = this_project["State"]
    project_workspace = this_project["Workspace"]
    workspace_name = project_workspace["Name"]
    workspace_oid = project_workspace["ObjectID"]

    data << project_id
    data << project_name
    data << project_state
    data << workspace_name
    data << workspace_oid

    project_csv << CSV::Row.new(PROJECT_CACHE_FIELDS, data)
  end

  @logger.info "Finished writing project cache to #{project_cache_filename}"
end

#write_subscription_cacheObject

Write subscription/workspace/project cache



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/rally_user_helper.rb', line 673

def write_subscription_cache()

  subscription_cache_filename = File.dirname(__FILE__) + "/" + SUB_CACHE
  @logger.info "Started writing subscription cache to #{subscription_cache_filename}"

  # Output CSV header
  subscription_csv = CSV.open(
    subscription_cache_filename,
    CACHE_WRITE_MODE,
    {:col_sep => CACHE_COL_DELIM, :encoding => @file_encoding}
  )
  subscription_csv << SUB_CACHE_FIELDS

  # Output cache to file
  # Record for CSV output
  @cached_subscription.each_pair do | sub_id, this_subscription |

    data = []
    @logger.info "sub_id: #{sub_id.inspect}"
    @logger.info "this_subscription: #{this_subscription.inspect}"

    data << sub_id
    data << this_subscription

    subscription_csv << CSV::Row.new(SUB_CACHE_FIELDS, data)
  end

  @logger.info "Finished writing subscription cache to #{subscription_cache_filename}"
end

#write_workspace_cacheObject



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/rally_user_helper.rb', line 703

def write_workspace_cache()
  workspace_cache_filename = File.dirname(__FILE__) + "/" + WORKSPACE_CACHE
  @logger.info "Started writing workspace cache to #{workspace_cache_filename}"

  # Output CSV header
  workspace_csv = CSV.open(
    workspace_cache_filename,
    CACHE_WRITE_MODE,
    {:col_sep => CACHE_COL_DELIM, :encoding => @file_encoding}
  )
  workspace_csv << WORKSPACE_CACHE_FIELDS

  # Output cache to file
  # Record for CSV output
  @cached_workspaces.each_pair do | workspace_id, this_workspace |

    data = []

    workspace_id = this_workspace["ObjectID"]
    workspace_name = this_workspace["Name"]
    workspace_state = this_workspace["State"]

    data << workspace_id
    data << workspace_name
    data << workspace_state

    workspace_csv << CSV::Row.new(WORKSPACE_CACHE_FIELDS, data)
  end

  @logger.info "Finished writing workspace cache to #{workspace_cache_filename}"
end