File: SOGoToolExpireUserSessions.m

package info (click to toggle)
sogo 5.12.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,828 kB
  • sloc: objc: 122,550; javascript: 75,288; sh: 2,352; ansic: 2,055; perl: 861; python: 777; sql: 307; makefile: 187; php: 43; xml: 27
file content (161 lines) | stat: -rw-r--r-- 4,153 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
150
151
152
153
154
155
156
157
158
159
160
161
/* SOGoToolExpireUserSessions.m - this file is part of SOGo
 *
 * Copyright (C) 2012-2021 Inverse inc.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <Foundation/NSArray.h>
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSUserDefaults.h>

#import <GDLAccess/EOAdaptorChannel.h>

#import <GDLContentStore/GCSChannelManager.h>
#import <GDLContentStore/NSURL+GCS.h>

#import <NGExtensions/NSNull+misc.h>

#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoSession.h>
#import <SOGo/SOGoOpenIdSession.h>

#import "SOGoTool.h"

@interface SOGoToolExpireUserSessions : SOGoTool
@end

@implementation SOGoToolExpireUserSessions

+ (NSString *) command
{
  return @"expire-sessions";
}

+ (NSString *) description
{
  return @"expires user sessions without activity for specified number of minutes";
}

- (void) usage
{
  fprintf (stderr, "expire-sessions [nbMinutes]\n\n"
	   "       nbMinutes       Number of minutes of inactivity after which a user session will be expired\n"
	   "\n"
     "The expire-sessions action should be configured as a cronjob.\n");
}

- (BOOL) expireUserSessionOlderThan: (int) nbMinutes
{
  BOOL rc;
  EOAdaptorChannel *channel;
  GCSChannelManager *cm;
  NSArray *attrs;
  NSDictionary *qresult;
  NSException *ex;
  NSString *sql, *sessionsFolderURL, *sessionID;
  NSURL *tableURL;
  NSUserDefaults *ud;

  unsigned int now, oldest;

  rc = YES;
  ud = [NSUserDefaults standardUserDefaults];
  now = [[NSCalendarDate calendarDate] timeIntervalSince1970];
  oldest = now - (nbMinutes * 60);
  sessionID = nil;

  sessionsFolderURL = [ud stringForKey: @"OCSSessionsFolderURL"];
  if (!sessionsFolderURL)
  {
    if (verbose)
      NSLog(@"Couldn't read OCSSessionsFolderURL");
    return rc = NO;
  }

  tableURL = [[NSURL alloc] initWithString: sessionsFolderURL];
  cm = [GCSChannelManager defaultChannelManager];
  channel = [cm acquireOpenChannelForURL: tableURL];
  if (!channel)
  {
    /* FIXME: nice error msg */
    NSLog(@"Can't aquire channel");
    return rc = NO;
  }

  sql = [NSString stringWithFormat: @"SELECT c_id FROM %@ WHERE c_lastseen <= %d",
                  [tableURL gcsTableName], oldest];
  ex = [channel evaluateExpressionX: sql]; 
  if (ex)
  {
    NSLog(@"%@", [ex reason]);
    [ex raise];
    return rc = NO;
  }

  attrs = [channel describeResults: NO];
  while ((qresult = [channel fetchAttributes: attrs withZone: NULL]))
    {
      sessionID = [qresult objectForKey: @"c_id"];
      if (sessionID)
        {
          if (verbose)
            NSLog(@"Removing session %@", sessionID);
          [SOGoSession deleteValueForSessionKey: sessionID];
        }
    }
  [cm releaseChannel: channel  immediately: YES];

  if (verbose && sessionID == nil)
    NSLog(@"No session to remove");

  return rc;
}

- (BOOL) run
{
  BOOL rc;
  int sessionExpireMinutes = -1;

  rc = NO;

  if ([arguments count])
  {
    sessionExpireMinutes = [[arguments objectAtIndex: 0] intValue];
  }

  NSLog(@"Remove all sessions older than %d min", sessionExpireMinutes);

  if (sessionExpireMinutes == 0 && ![[arguments objectAtIndex: 0] isEqualToString:@"0"])
  {
    //If the input is not a number intValue return 0 so we check that's really the case
    [self usage];
  }
  else if (sessionExpireMinutes >= 0)
  {
    rc = [self expireUserSessionOlderThan: sessionExpireMinutes];
  }
  else
  {
    [self usage];
  }
  
  return rc;
}

@end