Class: Collavre::SystemSetting

Inherits:
ApplicationRecord show all
Defined in:
app/models/collavre/system_setting.rb

Constant Summary collapse

CACHE_EXPIRY =

Cache expiry time for settings

5.minutes
DEFAULT_MAX_LOGIN_ATTEMPTS =

Default values for account lockout

5
DEFAULT_LOCKOUT_DURATION_MINUTES =
30
DEFAULT_SESSION_TIMEOUT_MINUTES =

Default values for session timeout (in minutes, 0 = no timeout)

0
DEFAULT_PASSWORD_MIN_LENGTH =

Default values for password policy

8
DEFAULT_PASSWORD_RESET_RATE_LIMIT =

Default values for rate limiting

5
DEFAULT_PASSWORD_RESET_RATE_PERIOD_MINUTES =
60
DEFAULT_API_RATE_LIMIT =
100
DEFAULT_API_RATE_PERIOD_MINUTES =
1
DEFAULT_CREATIVES_LOGIN_REQUIRED =

Default values for creatives access By default, public access is allowed (false)

false
DEFAULT_HOME_PAGE_PATH =

Default home page path for unauthenticated visitors (nil means use root_path “/”)

nil
DEFAULT_HOME_PAGE_PATH_AUTHENTICATED =

Default home page path for authenticated users. Signed-in visitors hitting “/” are redirected to this path when the admin has not configured a value. Set to “/” via the admin UI to disable the redirect and fall back to the unauthenticated rewrite.

"/creatives"
DEFAULT_LIGHT_THEME_ID =

Default theme IDs (nil means use built-in light/dark) These reference UserTheme IDs for admin-configured custom themes

nil
DEFAULT_DARK_THEME_ID =
nil
DEFAULT_LLM_REQUEST_TIMEOUT_SECONDS =

Default LLM request timeout (seconds)

1800
DEFAULT_DISPLAY_LEVEL =

Default creative display settings (system-wide)

6
DEFAULT_COMPLETION_MARK =
""

Class Method Summary collapse

Class Method Details

.api_rate_limitObject

Rate limiting settings - API



156
157
158
# File 'app/models/collavre/system_setting.rb', line 156

def self.api_rate_limit
  cached_value("api_rate_limit")&.to_i || DEFAULT_API_RATE_LIMIT
end

.api_rate_periodObject



164
165
166
# File 'app/models/collavre/system_setting.rb', line 164

def self.api_rate_period
  api_rate_period_minutes.minutes
end

.api_rate_period_minutesObject



160
161
162
# File 'app/models/collavre/system_setting.rb', line 160

def self.api_rate_period_minutes
  cached_value("api_rate_period_minutes")&.to_i || DEFAULT_API_RATE_PERIOD_MINUTES
end

.cached_value(key, default = nil) ⇒ Object

Cached setting retrieval



55
56
57
58
59
# File 'app/models/collavre/system_setting.rb', line 55

def self.cached_value(key, default = nil)
  Rails.cache.fetch("system_setting:#{key}", expires_in: CACHE_EXPIRY) do
    find_by(key: key)&.value
  end || default
end

.clear_all_cacheObject



61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/collavre/system_setting.rb', line 61

def self.clear_all_cache
  # Clear known setting keys
  %w[
    help_menu_link mcp_tool_approval_required max_login_attempts
    lockout_duration_minutes session_timeout_minutes password_min_length
    password_reset_rate_limit password_reset_rate_period_minutes
    api_rate_limit api_rate_period_minutes auth_providers_disabled
    creatives_login_required home_page_path home_page_path_authenticated default_light_theme_id default_dark_theme_id
    display_level completion_mark llm_request_timeout_seconds
  ].each { |k| Rails.cache.delete("system_setting:#{k}") }
end

.completion_markObject



194
195
196
197
# File 'app/models/collavre/system_setting.rb', line 194

def self.completion_mark
  value = cached_value("completion_mark")
  value.nil? ? DEFAULT_COMPLETION_MARK : value
end

.creatives_login_required?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/collavre/system_setting.rb', line 88

def self.
  cached_value("creatives_login_required", DEFAULT_CREATIVES_LOGIN_REQUIRED.to_s) == "true"
end

.default_dark_themeObject



184
185
186
187
# File 'app/models/collavre/system_setting.rb', line 184

def self.default_dark_theme
  id = default_dark_theme_id
  id ? Collavre::UserTheme.find_by(id: id) : nil
end

.default_dark_theme_idObject



174
175
176
177
# File 'app/models/collavre/system_setting.rb', line 174

def self.default_dark_theme_id
  value = cached_value("default_dark_theme_id")
  value.present? && value.to_i.positive? ? value.to_i : nil
end

.default_light_themeObject



179
180
181
182
# File 'app/models/collavre/system_setting.rb', line 179

def self.default_light_theme
  id = default_light_theme_id
  id ? Collavre::UserTheme.find_by(id: id) : nil
end

.default_light_theme_idObject

Default theme IDs for users without a personal theme preference



169
170
171
172
# File 'app/models/collavre/system_setting.rb', line 169

def self.default_light_theme_id
  value = cached_value("default_light_theme_id")
  value.present? && value.to_i.positive? ? value.to_i : nil
end

.display_levelObject

Creative display settings (system-wide)



190
191
192
# File 'app/models/collavre/system_setting.rb', line 190

def self.display_level
  cached_value("display_level")&.to_i || DEFAULT_DISPLAY_LEVEL
end


84
85
86
# File 'app/models/collavre/system_setting.rb', line 84

def self.help_menu_link
  cached_value("help_menu_link")
end

.home_page_pathObject



92
93
94
95
# File 'app/models/collavre/system_setting.rb', line 92

def self.home_page_path
  value = cached_value("home_page_path")
  value.presence
end

.home_page_path_authenticatedObject



97
98
99
100
# File 'app/models/collavre/system_setting.rb', line 97

def self.home_page_path_authenticated
  value = cached_value("home_page_path_authenticated")
  value.presence || DEFAULT_HOME_PAGE_PATH_AUTHENTICATED
end

.llm_request_timeout_secondsObject

LLM request timeout (seconds)



200
201
202
203
# File 'app/models/collavre/system_setting.rb', line 200

def self.llm_request_timeout_seconds
  value = cached_value("llm_request_timeout_seconds")&.to_i
  value.nil? || value < 30 ? DEFAULT_LLM_REQUEST_TIMEOUT_SECONDS : value
end

.lockout_durationObject



118
119
120
# File 'app/models/collavre/system_setting.rb', line 118

def self.lockout_duration
  lockout_duration_minutes.minutes
end

.lockout_duration_minutesObject



114
115
116
# File 'app/models/collavre/system_setting.rb', line 114

def self.lockout_duration_minutes
  cached_value("lockout_duration_minutes")&.to_i || DEFAULT_LOCKOUT_DURATION_MINUTES
end

.max_login_attemptsObject

Account lockout settings



110
111
112
# File 'app/models/collavre/system_setting.rb', line 110

def self.
  cached_value("max_login_attempts")&.to_i || DEFAULT_MAX_LOGIN_ATTEMPTS
end

.mcp_tool_approval_required?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
# File 'app/models/collavre/system_setting.rb', line 102

def self.mcp_tool_approval_required?
  if Current.mcp_tool_approval_required.nil?
    Current.mcp_tool_approval_required = cached_value("mcp_tool_approval_required") == "true"
  end
  Current.mcp_tool_approval_required
end

.password_min_lengthObject

Password policy settings (capped at 72 due to bcrypt limit)



123
124
125
126
127
# File 'app/models/collavre/system_setting.rb', line 123

def self.password_min_length
  value = cached_value("password_min_length")&.to_i
  value = DEFAULT_PASSWORD_MIN_LENGTH if value.nil? || value < 1
  [ value, 72 ].min
end

.password_reset_rate_limitObject

Rate limiting settings - Password Reset



143
144
145
# File 'app/models/collavre/system_setting.rb', line 143

def self.password_reset_rate_limit
  cached_value("password_reset_rate_limit")&.to_i || DEFAULT_PASSWORD_RESET_RATE_LIMIT
end

.password_reset_rate_periodObject



151
152
153
# File 'app/models/collavre/system_setting.rb', line 151

def self.password_reset_rate_period
  password_reset_rate_period_minutes.minutes
end

.password_reset_rate_period_minutesObject



147
148
149
# File 'app/models/collavre/system_setting.rb', line 147

def self.password_reset_rate_period_minutes
  cached_value("password_reset_rate_period_minutes")&.to_i || DEFAULT_PASSWORD_RESET_RATE_PERIOD_MINUTES
end

.session_timeoutObject



138
139
140
# File 'app/models/collavre/system_setting.rb', line 138

def self.session_timeout
  session_timeout_minutes.minutes
end

.session_timeout_enabled?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/models/collavre/system_setting.rb', line 134

def self.session_timeout_enabled?
  session_timeout_minutes > 0
end

.session_timeout_minutesObject

Session timeout settings



130
131
132
# File 'app/models/collavre/system_setting.rb', line 130

def self.session_timeout_minutes
  cached_value("session_timeout_minutes")&.to_i || DEFAULT_SESSION_TIMEOUT_MINUTES
end