Class: ICFS::Api

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

Overview

Api

Constant Summary collapse

ValSize =

Validate a size

{
  method: :integer,
  min: 2,
  max: 100
}.freeze
ValPage =

Validate a page

{
  method: :integer,
  min: 1,
  max: 10
}.freeze
ValPurpose =

Validate a purpose

{
  method: :string,
  min: 1,
  max: 32,
  invalid: /[[:cntrl:]]/.freeze
}.freeze
ValCaseSearch =

Validate a case search query

{
  method: :hash,
  optional: {
    title: Items::FieldTitle,
    tags: Items::FieldTagAny,
    status: Validate::IsBoolean,
    template: Validate::IsBoolean,
    grantee: Items::FieldUsergrp,
    perm: Items::FieldPermAny,
    size: ValSize,
    page: ValPage,
    purpose: ValPurpose,
  }.freeze,
}.freeze
ValLogSearch =

Validate a log search

{
  method: :hash,
  optional: {
    caseid: Items::FieldCaseid,
    after: Validate::IsIntPos,
    before: Validate::IsIntPos,
    user: Items::FieldUsergrp,
    case_edit: Validate::IsBoolean,
    entry: Validate::IsIntPos,
    index: Validate::IsIntPos,
    action: Validate::IsIntPos,
    size: ValSize,
    page: ValPage,
    purpose: ValPurpose,
    sort: {
      method: :string,
      allowed: Set[
        'time_desc',
        'time_asc',
      ].freeze,
      whitelist: true,
    }.freeze
  }.freeze
}.freeze
ValEntrySearch =

Validate an entry search query

{
  method: :hash,
  optional: {
    title: Items::FieldTitle,
    content: Items::FieldContent,
    tags: Items::FieldTagAny,
    caseid: Items::FieldCaseid,
    action: Validate::IsIntPos,
    index: Validate::IsIntPos,
    after: Validate::IsIntPos,
    before: Validate::IsIntPos,
    stat: Items::FieldStat,
    credit: Items::FieldUsergrp,
    size: ValSize,
    page: ValPage,
    purpose: ValPurpose,
    sort: {
      method: :string,
      allowed: Set[
        'time_desc',
        'time_asc',
      ].freeze,
      whitelist: true,
    }.freeze
  }.freeze
}.freeze
ValActionSearch =

Validate a task search

{
  method: :hash,
  required: {
    assigned: {
      method: :any,
      check: [
        Items::FieldUsergrp,
        {
          method: :equals,
          check: ICFS::UserCase
        }
      ].freeze
    }.freeze
  }.freeze,
  optional: {
    caseid: Items::FieldCaseid,
    title: Items::FieldTitle,
    status: Validate::IsBoolean,
    flag: Validate::IsBoolean,
    before: Validate::IsIntPos,
    after: Validate::IsIntPos,
    tags: Items::FieldTagAny,
    size: ValSize,
    page: ValPage,
    purpose: ValPurpose,
    sort: {
      method: :string,
      allowed: Set[
        'time_desc',
        'time_asc',
      ].freeze,
      whitelist: true,
    }.freeze
  }.freeze
}.freeze
ValIndexSearch =

Validate an index search query

{
  method: :hash,
  optional: {
    caseid: Items::FieldCaseid,
    title: Items::FieldTitle,
    prefix: Items::FieldTitle,
    content: Items::FieldContent,
    tags: Items::FieldTagAny,
    size: ValSize,
    page: ValPage,
    purpose: ValPurpose,
    sort: {
      method: :string,
      allowed: Set[
        'title_desc',
        'title_asc',
        'index_desc',
        'index_asc',
      ].freeze,
      whitelist: true,
    }.freeze
  }.freeze
}.freeze
ValStatsSearch =

validate the stats query

{
  method: :hash,
  optional: {
    caseid: Items::FieldCaseid,
    after: Validate::IsIntPos,
    before: Validate::IsIntPos,
    credit: Items::FieldUsergrp,
    purpose: ValPurpose,
  }.freeze
}.freeze
ValCaseTags =

Case Tags search validation

{
  method: :hash,
  optional: {
    status: Validate::IsBoolean,
    template: Validate::IsBoolean,
    grantee: Items::FieldUsergrp,
    purpose: ValPurpose,
  }.freeze,
}.freeze
ValEntryTags =

Entry Tags search validation

{
  method: :hash,
  required: {
    caseid: Items::FieldCaseid,
  }.freeze,
  optional: {
    purpose: ValPurpose,
  }.freeze,
}.freeze
ValActionTags =

Task Tags search validation

{
  method: :hash,
  required: {
    assigned: {
      method: :any,
      check: [
        Items::FieldUsergrp,
        {
          method: :equals,
          check: ICFS::UserCase
        }.freeze
      ].freeze
    }.freeze,
  }.freeze,
  optional: {
    caseid: Items::FieldCaseid,
    status: Validate::IsBoolean,
    flag: Validate::IsBoolean,
    before: Validate::IsIntPos,
    after: Validate::IsIntPos,
    purpose: ValPurpose,
  }.freeze,
}.freeze
ValIndexTags =

Validate a index tag search

{
method: :hash,
  required: {
    caseid: Items::FieldCaseid,
  }.freeze,
  optional: {
    purpose: ValPurpose,
  }.freeze
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stats, users, cache, store, config = nil) ⇒ Api

New API

Parameters:

  • stats (Array<String>)

    Global stats

  • users (Users)

    the User/role/group interface

  • cache (Cache)

    the cache

  • store (Store)

    the store

  • config (Config) (defaults to: nil)

    the config settings, optional



56
57
58
59
60
61
62
63
# File 'lib/icfs/api.rb', line 56

def initialize(stats, users, cache, store, config=nil)
  @users = users
  @cache = cache
  @store = store
  @gstats = stats.map{|st| st.dup.freeze }.freeze
  @config = config
  reset
end

Instance Attribute Details

#configObject (readonly)

Config settings



154
155
156
# File 'lib/icfs/api.rb', line 154

def config
  @config
end

#groupsObject (readonly)

Groups



132
133
134
# File 'lib/icfs/api.rb', line 132

def groups
  @groups
end

#gstatsObject (readonly)

Globals stats



143
144
145
# File 'lib/icfs/api.rb', line 143

def gstats
  @gstats
end

#permsObject (readonly)

Global perms



138
139
140
# File 'lib/icfs/api.rb', line 138

def perms
  @perms
end

#rolesObject (readonly)

Roles



126
127
128
# File 'lib/icfs/api.rb', line 126

def roles
  @roles
end

#urgObject (readonly)

User, Roles, Groups set



149
150
151
# File 'lib/icfs/api.rb', line 149

def urg
  @urg
end

#userObject

User



120
121
122
# File 'lib/icfs/api.rb', line 120

def user
  @user
end

Instance Method Details

#access_list(cid) ⇒ Set<String>

Get an access list

Parameters:

  • cid (String)

    caseid

Returns:

  • (Set<String>)

    the perms granted the user for this case

Raises:



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/icfs/api.rb', line 199

def access_list(cid)
  if !@access.key?(cid)

    # get grants for the case
    cse = case_read(cid)
    al = Set.new
    cse['access'].each do |ac|
      gs = Set.new(ac['grant'])
      al.add(ac['perm']) if @urg.intersect?(gs)
    end

    # higher perms imply lower ones
    al.add(ICFS::PermRead) if al.include?(ICFS::PermManage)
    al.add(ICFS::PermWrite) if al.include?(ICFS::PermAction)
    al.add(ICFS::PermRead) if al.include?(ICFS::PermWrite)

    # merge in global perms
    al.merge @perms

    @access[cid] = al
  end
  return @access[cid]
end

#action_read(cid, anum, lnum = 0) ⇒ Action

Read an action

Parameters:

  • cid (String)

    caseid

  • anum (Integer)

    the action number

  • lnum (Integer) (defaults to: 0)

    the log number or 0 for current

Returns:

  • (Action)

    requested action

Raises:



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/icfs/api.rb', line 412

def action_read(cid, anum, lnum=0)

  # get current action
  ac = _action_read(cid, anum)

  # see if we can read the action
  unless _can_read?(cid, anum)
    raise(Error::Perms, 'missing perms: %s' % ICFS::PermRead )
  end

  # return the requested version
  if( lnum == 0 || ac['log'] == lnum )
    return ac
  else
    json = @store.action_read( cid, anum, lnum)
    return Items.parse(json, 'action', Items::ItemAction)
  end
end

#action_search(query) ⇒ Object

Search for actions



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/icfs/api.rb', line 672

def action_search(query)
  Items.validate(query, 'Action Search', ValActionSearch)

  # permissions check
  # - have global search permissions / read access to the case
  # - searching for role you have
  unless( _search?(query) || @ur.include?(query[:assigned]) ||
     (query[:assigned] == ICFS::UserCase && query[:caseid] &&
      access_list(query[:caseid]).include?(ICFS::PermAction) ))
    raise(Error::Perms, 'Does not have permission to search')
  end

  # run the search
  return @cache.action_search(query)
end

#action_tags(query) ⇒ Object

Get action tags



851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'lib/icfs/api.rb', line 851

def action_tags(query)
  Items.validate(query, 'Task Tags Search', ValActionTags)

  # only allow searches for user/roles you have
  unless @ur.include?(query[:assigned]) ||
     (query[:assigned] == ICFS::UserCase && query[:caseid] &&
        access_list(query[:caseid]).include?(ICFS::PermAction) )
    raise(Error::Perms, 'May not search for other\'s tasks')
  end

  # run the search
  return @cache.action_tags(query)
end

#case_create(ent, cse, tid = nil, unam = nil) ⇒ Object

Create a new case

Parameters:

  • ent (Hash)

    the first entry

  • cse (Hash)

    the case

  • tid (String) (defaults to: nil)

    the template name

  • unam (String) (defaults to: nil)

    the user name if not using the Users API

Raises:

  • (ArgumentError)


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
1005
1006
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
# File 'lib/icfs/api.rb', line 903

def case_create(ent, cse, tid=nil, unam=nil)

  ####################
  # Sanity checks

  # form & values
  Items.validate(ent, 'entry', Items::ItemEntryNew)
  Items.validate(cse, 'case', Items::ItemCaseEdit)

  # get caseid
  cid = ent['caseid']
  cid ||= cse['caseid']
  unless cid
    raise(Error::Values, 'No caseid provided')
  end

  # access users/roles/groups are valid, unless manually specifying user
  unless unam
    cse["access"].each do |acc|
      acc["grant"].each do |gnt|
        urg = @users.read(gnt)
        if !urg
          raise(Error::NotFound, 'User/role/group %s not found' % urg)
        end
      end
    end
  end

  # permissions
  perms = Set[ ICFS::PermManage ]
  perms.merge(ent['perms']) if ent['perms']

  # template
  if tid
    tmpl = case_read(tid)
    unless tmpl['template']
      raise(Error::Perms, 'Not a template')
    end

    al = access_list(tid)
    unless al.include?(ICFS::PermManage)
      raise(Error::Perms, 'May not create cases from this template')
    end
  end

  # no action/indexes
  if ent['action']
    raise(Error::Value, 'No Action for a new case entry')
  end
  if ent['index']
    raise(Error::Value, 'No Index for a new case entry')
  end

  # Allow case creation without a Users system in place
  user = @user ? @user : unam
  raise(ArgumentError, 'No user specified') if user.nil?

  ####################
  # Prep

  # case
  cse['icfs'] = 1
  cse['caseid'] = cid
  cse['log'] = 1
  cse['tags'] ||= [ ICFS::TagNone ]
  cse['entry'] = 1
  citem = Items.generate(cse, 'case', Items::ItemCase)

  # entry
  ent['icfs'] = 1
  ent['entry'] = 1
  ent['log'] = 1
  ent['tags'] ||= [ ]
  ent['tags'] << ICFS::TagCase
  ent['user'] = user
  files, fhash = _pre_files(ent)

  # log
  log = {
    'icfs' => 1,
    'caseid' => cid,
    'log' => 1,
    'prev' => '0'*64,
    'user' => user,
    'entry' => {
      'num' => 1,
     },
     'case' => {
       'set' => true,
       'hash' => ICFS.hash(citem),
     },
  }
  log['files_hash'] = fhash if fhash

  # current
  cur = {
    'icfs' => 1,
    'caseid' => cid,
    'log' => 1,
    'entry' => 1,
    'action' => 0,
    'index' => 0
  }

  ####################
  # Write the case

  # take lock
  @cache.lock_take(cid)
  begin
    if @cache.case_read(cid)
      raise(Error::Conflict, 'Case already exists')
    end

    now = Time.now.to_i

    # finish items
    ent['time'] ||= now
    ent['files'].each{|fi| fi['log'] ||= 1 } if ent['files']
    eitem = Items.generate(ent, 'entry', Items::ItemEntry)
    log['time'] = now
    log['entry']['hash'] = ICFS.hash(eitem)
    litem = Items.generate(log, 'log', Items::ItemLog)
    cur['hash'] = ICFS.hash(litem)
    nitem = Items.generate(cur, 'current', Items::ItemCurrent)

    # write to cache
    @cache.entry_write(cid, 1, eitem)
    @cache.log_write(cid, 1, litem)
    @cache.case_write(cid, citem)
    @cache.current_write(cid, nitem)

    # write to store
    @store.entry_write(cid, 1, 1, eitem)
    @store.log_write(cid, 1, litem)
    @store.case_write(cid, 1, citem)

  # release lock
  ensure
    @cache.lock_release(cid)
  end

  # files
  files.each_index{|ix| @store.file_write(cid, 1, 1, ix+1, files[ix]) }

end

#case_read(cid, lnum = 0) ⇒ Case

Read a case

Parameters:

  • cid (String)

    caseid

  • lnum (Integer) (defaults to: 0)

    log it was recorded

Returns:

  • (Case)

    the case

Raises:



294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/icfs/api.rb', line 294

def case_read(cid, lnum=0)
  if lnum != 0
    json = @store.case_read(cid, lnum)
    return Items.parse(json, 'case', Items::ItemCase)
  end

  if !@cases.key?(cid)
    json = @cache.case_read(cid)
    cur = Items.parse(json, 'case', Items::ItemCase)
    @cases[cid] = cur
  end
  return @cases[cid]
end

#case_search(query) ⇒ Object

Search for a case

Parameters:

  • query (Hash)

    a query



507
508
509
510
# File 'lib/icfs/api.rb', line 507

def case_search(query)
  Items.validate(query, 'Case Search', ValCaseSearch)
  @cache.case_search(query)
end

#case_tags(query) ⇒ Object

Get case tags



789
790
791
792
# File 'lib/icfs/api.rb', line 789

def case_tags(query)
  Items.validate(query, 'Case Tags Search', ValCaseTags)
  return @cache.case_tags(query)
end

#current_read(cid) ⇒ Object

Read a current

Parameters:

  • cid (String)

    caseid



468
469
470
471
472
473
474
475
476
477
# File 'lib/icfs/api.rb', line 468

def current_read(cid)

  al = access_list(cid)
  if !al.include?(ICFS::PermRead)
    raise(Error::Perms, 'missing perms: %s' % ICFS::PermRead)
  end

  json = @cache.current_read(cid)
  return Items.parse(json, 'current', Items::ItemCurrent)
end

#entry_read(cid, enum, lnum = 0) ⇒ Object

Read an entry

Parameters:

  • cid (String)

    caseid

  • enum (Integer)

    the entry number

  • lnum (Integer) (defaults to: 0)

    the log number or 0 for current

Raises:



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/icfs/api.rb', line 340

def entry_read(cid, enum, lnum=0)

  # get access list and current entry
  al = access_list(cid)
  json = @cache.entry_read(cid, enum)
  ec = Items.parse(json, 'entry', Items::ItemEntry)

  # see if we can read the entry
  need = Set.new
  need.add( ICFS::PermRead ) unless _can_read?(cid, ec['action'] )
  need.merge(ec['perms']) if ec['perms']
  need.subtract(al)
  unless need.empty?
    raise(Error::Perms, 'missing perms: %s' %
      need.to_a.sort.join(', ') )
  end

  # return requested version
  if( lnum == 0 || ec['log'] == lnum )
    return ec
  else
    json = @store.entry_read(cid, enum, lnum)
    return Items.parse(json, 'entry', Items::ItemEntry)
  end
end

#entry_search(query) ⇒ Object

Search for entries



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
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
628
# File 'lib/icfs/api.rb', line 584

def entry_search(query)
  Items.validate(query, 'Entry Search', ValEntrySearch)

  # check permissions
  # - have global search permissions / read access to the case
  # - are searching for an action they can read
  unless( _search?(query) || (query[:caseid] &&
      query[:action] && tasked?(query[:caseid], query[:action])))
    raise(Error::Perms, 'Does not have permission to search')
  end

  # run the query
  res = @cache.entry_search(query)

  # check perms for each entry
  res[:list].each do |se|
    ent = se[:object]

    # can not read the case/action - basically nothing
    unless _can_read?(ent[:caseid], ent[:action])
      ent[:time] = nil
      ent[:title] = nil
      ent[:perms] = nil
      ent[:action] = nil
      ent[:tags] = nil
      ent[:files] = nil
      ent[:stats] = nil
      se[:snippet] = nil
      next
    end

    # can read the case/action, missing perms for this entry
    # leave time, perms, and action
    al = access_list(ent[:caseid])
    if !(Set.new(ent[:perms]) - al).empty?
      ent[:title] = nil
      ent[:tags] = nil
      ent[:files] = nil
      ent[:stats] = nil
      se[:snippet] = nil
    end
  end

  return res
end

#entry_tags(query) ⇒ Object

Get entry tags



810
811
812
813
814
815
816
817
818
819
# File 'lib/icfs/api.rb', line 810

def (query)
  Items.validate(query, 'Entry Tags Search', ValEntryTags)

  # permissions
  # - read access to case
  unless access_list(query[:caseid]).include?(ICFS::PermRead)
    raise(Error::Perms, 'missing perms: %s' % ICFS::PermRead)
  end
  return @cache.(query)
end

#file_read(cid, enum, lnum, fnum) ⇒ Object

Read a file

Parameters:

  • cid (String)

    caseid

  • enum (Integer)

    the entry number

  • lnum (Integer)

    the log number

  • fnum (Integer)

    the file number

Raises:



377
378
379
380
381
382
# File 'lib/icfs/api.rb', line 377

def file_read(cid, enum, lnum, fnum)
  entry_read(cid, enum)
  fi = @store.file_read(cid, enum, lnum, fnum)
  raise(Error::NotFound, 'file not found') if !fi
  return fi
end

#index_read(cid, xnum, lnum = 0) ⇒ Object

Read an index

Parameters:

  • cid (String)
  • xnum (Integer)

    the index number

  • lnum (Integer) (defaults to: 0)

    the log number

Raises:



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/icfs/api.rb', line 441

def index_read(cid, xnum, lnum=0)

  # get access list
  al = access_list(cid)
  if !al.include?(ICFS::PermRead)
    raise(Error::Perms, 'missing perms: %s' % ICFS::PermRead )
  end

  # read curent index
  json = @cache.index_read(cid, xnum)
  xc = Items.parse(json, 'index', Items::ItemIndex)

  # return the requested version
  if( lnum == 0 || xc['log'] == lnum )
    return xc
  else
    json = @store.index_read(cid, xnum, lnum)
    return Items.parse(json, 'index', Items::ItemIndex)
  end
end

#index_search(query) ⇒ Object

Search for indexes



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/icfs/api.rb', line 718

def index_search(query)
  Items.validate(query, 'Index Search', ValIndexSearch)

  # permissions check
  # - have global search permissions / read access to the case
  unless _search?(query)
    raise(Error::Perms, 'Do not have permission to search')
  end

  # run the query
  res = @cache.index_search(query)

  # check perms for each index
  res[:list].each do |se|
    idx = se[:object]

    unless access_list(idx[:caseid]).include?(ICFS::PermRead)
      idx[:title] = nil
      idx[:tags] = nil
    end
  end

  return res
end

#index_tags(query) ⇒ Object

Get index tags



881
882
883
884
885
886
887
# File 'lib/icfs/api.rb', line 881

def index_tags(query)
  Items.validate(query, 'Index Tags', ValIndexTags)
  unless access_list(query[:caseid]).include?(ICFS::PermRead)
    raise(Error::Perms, 'missing perms: %s' % ICFS::PermRead)
  end
  return @cache.index_tags(query)
end

#log_read(cid, lnum) ⇒ Object

Read a log

Parameters:

  • cid (String)

    caseid

  • lnum (Integer)

    log number

Raises:



317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/icfs/api.rb', line 317

def log_read(cid, lnum)

  # get access list
  al = access_list(cid)
  if !al.include?(ICFS::PermRead)
    raise(Error::Perms, 'missing perms: %s' % ICFS::PermRead)
  end

  # read
  json = @cache.log_read(cid, lnum)
  return Items.parse(json, 'log', Items::ItemLog)
end

#log_search(query) ⇒ Object

Search for a log

Parameters:

  • query (Hash)

    a query



545
546
547
548
# File 'lib/icfs/api.rb', line 545

def log_search(query)
  Items.validate(query, 'Log Search', ValLogSearch)
  @cache.log_search(query)
end

#record(ent, act, idx, cse) ⇒ Object

Write items to a case

Parameters:

  • ent (Hash)

    Entry to record, optional

  • act (Hash, Nilclass)

    Action to record, optional

  • idx (Hash, Nilclass)

    Index to record, optional

  • cse (Hash, Nilclass)

    Case to record, optional



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
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
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
1170
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
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
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
1275
1276
1277
1278
1279
1280
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
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
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
1398
1399
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
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
# File 'lib/icfs/api.rb', line 1059

def record(ent, act, idx, cse)

  ####################
  # Sanity checks

  # form & content
  if ent
    if (act || idx || cse)
      Items.validate(ent, 'New Entry', Items::ItemEntryNew)
    else
      Items.validate(ent, 'Editable Entry', Items::ItemEntryEdit)
    end
  end
  Items.validate(act, 'action', Items::ItemActionEdit) if act
  Items.validate(idx, 'index', Items::ItemIndexEdit) if idx
  Items.validate(cse, 'case', Items::ItemCaseEdit) if cse

  # get caseid
  cid ||= ent['caseid'] if ent
  cid ||= cse['caseid'] if cse
  cid ||= act['caseid'] if act
  cid ||= idx['caseid'] if idx
  unless cid
    raise(Error::Values, 'No caseid provided')
  end

  # no conflicting caseids
  if( (cse && cse['caseid'] && cse['caseid'] != cid) ||
      (act && act['caseid'] && act['caseid'] != cid) ||
      (idx && idx['caseid'] && idx['caseid'] != cid) )
    raise(Error::Values, 'Conflicting caseids provided')
  end

  # no changing the action
  if( act && ent && ent['action'] && act['action'] &&
      act['action'] != ent['action'] )
    raise(Error::Conflict, 'May not change entry\'s action')
  end

  # access users/roles/groups are valid
  if cse
    cse['access'].each do |acc|
      acc['grant'].each do |gnt|
        urg = @users.read(gnt)
        if !urg
          raise(Error::NotFound, 'User/role/group %s not found' % gnt)
        end
      end
    end
  end

  # tasking users/roles are valid
  if act
    act['tasks'].each_index do |ix|
      next if ix == 0
      tsk = act['tasks'][ix]
      ur = @users.read(tsk['assigned'])
      if !ur
        raise(Error::NotFound, 'User/role %s not found' %
           tsk['assigned'])
      end
      type = ur['type']
      if type != 'user' && type != 'role'
        raise(Error::Values, 'Not a user or role: %s' %
           tsk['assigned'])
      end
    end
  end


  ####################
  # Prep

  # entry
  if ent
    ent['icfs'] = 1
    ent['tags'] ||= [ ]
    ent['user'] = @user
    ent['caseid'] = cid
    files, fhash = _pre_files(ent)
  else
    files = []
  end

  # action
  if act
    ent['tags'] << ICFS::TagAction if ent
    act['icfs'] = 1
    act['caseid'] = cid
    act['tasks'].each do |tk|
      tk['tags'] ||= [ ICFS::TagNone ]
    end
  end

  # index
  if idx
    ent['tags'] << ICFS::TagIndex if ent
    idx['icfs'] = 1
    idx['caseid'] = cid
    idx['tags'] ||= [ ICFS::TagNone ]
  end

  # case
  if cse
    ent['tags'] << ICFS::TagCase if ent
    cse['icfs'] = 1
    cse['caseid'] = cid
    cse['tags'] ||= [ ICFS::TagNone ]
  end

  # log
  log = {
    'icfs' => 1,
    'caseid' => cid,
    'user' => @user,
  }
  log['files_hash'] = fhash if fhash

  # no tags
  ent['tags'] = [ ICFS::TagNone ] if ent && ent['tags'].empty?

  # current
  nxt = {
    'icfs' => 1,
    'caseid' => cid,
  }


  ####################
  # Write

  # take lock
  @cache.lock_take(cid)
  begin
    now = Time.now.to_i

    ####################
    # get prior items & numbers

    # current
    json = @cache.current_read(cid)
    cur = Items.parse(json, 'current', Items::ItemCurrent)

    # entry
    if ent
      if ent['entry']
        enum = ent['entry']
        json = @cache.entry_read(cid, enum)
        ent_pri = Items.parse(json, 'entry', Items::ItemEntry)
        nxt['entry'] = cur['entry']
      else
        enum = cur['entry'] + 1
        nxt['entry'] = enum
      end
    else
      nxt['entry'] = cur['entry']
    end

    # action
    if ent_pri && ent_pri['action']
      anum = ent_pri['action']
    elsif act && act['action']
      anum = act['action']
    elsif ent && ent['action']
      anum = ent['action']
    end
    if anum
      json = @cache.action_read(cid, anum)
      act_pri = Items.parse(json, 'action', Items::ItemAction)
      nxt['action'] = cur['action']
    elsif act
      anum = cur['action'] + 1
      nxt['action'] = anum
    else
      nxt['action'] = cur['action']
    end

    # index
    if idx
      if idx['index']
        xnum = idx['index']
        nxt['index'] = cur['index']
      else
        xnum = cur['index'] + 1
        nxt['index'] = xnum
      end
    else
      xnum = nil
      nxt['index'] = cur['index']
    end

    # case
    cse_pri = case_read(cid)
    al = access_list(cid)

    # log
    lnum = cur['log'] + 1
    nxt['log'] = lnum


    ####################
    # Checks
    perms = Set.new

    # entry
    perms.merge(ent['perms']) if ent && ent['perms']
    if ent_pri

      # must have those perms
      perms.add(ent_pri['perms']) if ent_pri['perms']

      # may not change action
      if ent_pri['action'] && (ent['action'] != ent_pri['action'])
        raise(Error::Conflict, 'May not change entry\'s action')
      end

      # may not remove or add action, index, case tags
      if( (ent_pri['tags'].include?(ICFS::TagAction) !=
               ent['tags'].include?(ICFS::TagAction) ) ||
          (ent_pri['tags'].include?(ICFS::TagIndex) !=
               ent['tags'].include?(ICFS::TagIndex) ) ||
          (ent_pri['tags'].include?(ICFS::TagCase) !=
               ent['tags'].include?(ICFS::TagCase) ) )
        raise(Error::Conflict, 'May not change entry\'s special tags')
      end
    end

    # action
    if act
      pri_tsk = act_pri ? act_pri['tasks'] : []
      cur_tsk = act['tasks']
      act_open = cur_tsk[0]['status']

      # not allowed to delete tasks
      if pri_tsk.size > cur_tsk.size
        raise(Error::Conflict, 'May not delete tasks')
      end

      # check each task
      perm_act = al.include?(ICFS::PermAction)
      tasked = false
      cur_tsk.each_index do |ix|
        ct = cur_tsk[ix]
        pt = pri_tsk[ix]

        # may not delete a tasking
        if pt && pt['assigned'] != ct['assigned']
          raise(Error::Conflict, 'May not delete task')
        end

        # new taskings require action to be open
        if !pt && !act_open
          raise(Error::Value, 'New tasks require the action be open')
        end

        # may not have a task open if action is closed
        if ct['status'] && !act_open
          raise(Error::Value, 'Open tasks on closed action')
        end

        # can set any values for our tasks
        if @ur.include?(ct['assigned']) || (ix == 0 && perm_act )
          tasked = true
          next
        end

        # must be flagged if new tasking or re-opening
        if !ct['flag'] && (!pt || (ct['status'] && !pt['status']))
          raise(Error::Value, 'New or re-opened taskings must flag')
        end

        # no changing other's taskings, no deflagging, and no
        # closing task without action
        if pt && (
           (pt['title'] != ct['title']) || (pt['time'] != ct['time']) ||
           (pt['tags'] != ct['tags']) || (pt['flag'] && !ct['flag']) ||
           (pt['status'] && !ct['status'] && !perm_act) )
          raise(Error::Value, 'May not change other\'s tasks')
        end
      end

      # new tasks or changes to other's tasks
      if !act_pri || !tasked
        perms.add( ICFS::PermAction )
      end

    end

    # no checks for index

    # case
    if cse
      # no changing template
      unless cse['template'] == cse_pri['template']
        raise(Error::Conflict, 'May not change template status')
      end

      # manage required
      perms.add( ICFS::PermManage ) if cse
    end

    # write unless a case or pre-existing action
    unless cse || act_pri
      perms.add( ICFS::PermWrite )
    end

    # permissions
    perms_miss = perms - al
    unless perms_miss.empty?
      raise(Error::Perms, 'Missing perms: %s' %
        perms_miss.to_a.sort.join(', ') )
    end


    ####################
    # Items

    # entry
    if ent
      ent['entry'] = enum
      ent['log'] = lnum
      ent['time'] ||= now
      ent['action'] = anum if act
      if idx
        if ent['index']
          ent['index'] << xnum
        else
          ent['index'] = [ xnum ]
        end
      end
      ent['index'].sort!.uniq! if ent['index']
      ent['files'].each{|fi| fi['log'] ||= lnum } if ent['files']
      eitem = Items.generate(ent, 'entry', Items::ItemEntry)
      log['entry'] = {
        'num' => enum,
        'hash' => ICFS.hash(eitem)
      }
    end

    # action
    if act
      act['action'] = anum
      act['log'] = lnum
      act['entry'] = enum if ent
      aitem = Items.generate(act, 'action', Items::ItemAction)
      log['action'] = {
        'num' => anum,
        'hash' => ICFS.hash(aitem)
      }
    end

    # index
    if idx
      idx['index'] = xnum
      idx['log'] = lnum
      idx['entry'] = enum if ent
      xitem = Items.generate(idx, 'index', Items::ItemIndex)
      log['index'] = {
        'num' => xnum,
        'hash' => ICFS.hash(xitem)
      }
    end

    # case
    if cse
      cse['log'] = lnum
      cse['entry'] = enum if ent
      citem = Items.generate(cse, 'case', Items::ItemCase)
      log['case'] = {
        'set' => true,
        'hash' => ICFS.hash(citem),
      }
    end

    # log
    log['log'] = lnum
    log['prev'] = cur['hash']
    log['time'] = now
    litem = Items.generate(log, 'log', Items::ItemLog)
    nxt['hash'] = ICFS.hash(litem)

    # next
    nitem = Items.generate(nxt, 'current', Items::ItemCurrent)


    ####################
    # Write

    # entry
    if ent
      @cache.entry_write(cid, enum, eitem)
      @store.entry_write(cid, enum, lnum, eitem)
    end

    # action
    if act
      @cache.action_write(cid, anum, aitem)
      @store.action_write(cid, anum, lnum, aitem)
    end

    # index
    if idx
      @cache.index_write(cid, xnum, xitem)
      @store.index_write(cid, xnum, lnum, xitem)
    end

    # case
    if cse
      @cache.case_write(cid, citem)
      @store.case_write(cid, lnum, citem)
    end

    # log
    @cache.log_write(cid, lnum, litem)
    @store.log_write(cid, lnum, litem)

    # current
    @cache.current_write(cid, nitem)

  # release the lock
  ensure
    @cache.lock_release(cid)
  end

  # write the files
  files.each_index{|ix| @store.file_write(cid, enum, lnum, ix+1, files[ix]) }

end

#resetObject

Reset the cached cases and access



160
161
162
163
164
165
# File 'lib/icfs/api.rb', line 160

def reset
  @cases = {}
  @access = {}
  @actions = {}
  @tasked = {}
end

#stats(query) ⇒ Object

Analyze stats



760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/icfs/api.rb', line 760

def stats(query)
  Items.validate(query, 'Stats Search', ValStatsSearch)

  # permissions check
  # - have global search permissions / read access to the case
  # - are searching for a user/role/group you have
  unless _search?(query) || (query[:credit] && @urg.include?(query[:credit]))
    raise(Error::Perms, 'Do not have permissions to search')
  end

  @cache.stats(query)
end

#stats_list(cid) ⇒ Set<String>

Get a stats list

Parameters:

  • cid (String)

    caseid

Returns:

  • (Set<String>)

    the stats, global and case

Raises:



183
184
185
186
187
188
189
# File 'lib/icfs/api.rb', line 183

def stats_list(cid)
  cse = case_read(cid)
  stats = Set.new
  stats.merge( cse['stats'] ) if cse['stats']
  stats.merge( @gstats )
  return stats
end

#tasked?(cid, anum) ⇒ Boolean

See if we are tasked

Returns:

  • (Boolean)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/icfs/api.rb', line 226

def tasked?(cid, anum)
  id = '%s.%d' % [cid, anum]
  unless @tasked.key?(id)
    act = _action_read(cid, anum)

    tasked = false
    act['tasks'].each do |tk|
      if @ur.include?(tk['assigned'])
        tasked = true
        break
      end
    end

    @tasked[id] = tasked
  end

  return @tasked[id]
end

#tempfileObject

Get a tempfile



171
172
173
# File 'lib/icfs/api.rb', line 171

def tempfile
  @store.tempfile
end

#user_flushObject

Flush any user caching



111
112
113
114
# File 'lib/icfs/api.rb', line 111

def user_flush()
  @users.flush(@user)
  self.user = @user
end