Skip to content

Commit

Permalink
feat: Implemented a SRAPI to generate API Point records, secured by r…
Browse files Browse the repository at this point in the history
…ole and ACL (#41)

feat: Added new SI for API support, staring with support for name/ID as input
feat: Added support for multiple points in an API request
fix: Added a semicolon on a line in PointsThing because the linter warning bothered me
  • Loading branch information
SapphicFire authored Oct 19, 2023
1 parent a8be5d2 commit 2088ffe
Show file tree
Hide file tree
Showing 15 changed files with 791 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 9c7718cddb229150791d8f8d13961979/checksum.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gWKRu7QBITFvh1b_7vWR3-Y5abzX63ToZrFUEEqbxNVkPM9zpg60x7eiSD8OXDY-Dgl5hGG7txrkCtXUXHvLL2VQPXFn3z9gc-8N5mPmJBpYm-lZgwLARSoRY-y_uhoCEYzAl3noLKxCv8F4a48YsH2EmI_Tk_vPaaT33vyiXU25lLkg_2w7niMeLhEuRV_n0xsa9B8gxbQUBzUcOA4V4U723Y4zh1LkSaOkc7Bou84LbS_dm2ma5rf2SH4tg11oWHwQQi-bmtaRjmgR198nI82TBqyTUKpZ_ltDCqZn8MRpMp2WCBBmJT7k-CbfHOltuHNzOHYZkpE7MuaMCfG-F7f6cxswgvqEq4b-dtkPhwzbu_B8yZ6tsBMeZN_mIsovk-VZncRLuY8Wk_BmIE_bsAIjaW0IhDhbwGvlG1udY15v95SfgSgziosl35tppJWtPaQG2uKu-p53Flzdc3C_Kc4W6ICOyZO-Vf1fdH_LRTJe6cEzITXIoiii6WgyW1xWZlGcCTzCG89A4KQMpncr1prM4BljOqct6Cz2sA5Yljgl6Zm_-Tq21vIICO8vXNmYYeDW9y0xJmRgnXW3kqxj93eIzNpetvKmp-Fr5gd4b0K_MCJAFuPt4gjuX63wiFeUctV36_Lk9dHpLBnotbfRjZ3h_vGirTMDa3jpc8QUhgU
zvZQkEauFqKLXB29GmcVnT-3P2E9aV4SR4bI08kuqgJ6ewg9crjNNf5xraVoRm9gw4tqKr4wu7KShzfO30Uk3Zv4HCm33W7i-Eetvscq9G8pR-_4e_deve10qLK9tNH1g1hlRshJAxxJ9dJl7r0bv11-Ch7tYQYDWN5sK1YphWxEzW7yTJO4oOeTf-ZmWAk3P4i0fJYep4kPVIWMel6UgW8OktVo6HOfjLDO2QtQ61OJ3_9yazLS9whH6BPIe7fOaIRCtYoN4Z9d7EDYtQOpLTDb2iINHzK929O97WsWgG1i8G6pY3SXnoXwu7qbbdfB809nVD66l8qBdAA1nWOK9XfpYWbvmEaQwnanpjF-p55m31F0LRudgv4KAyKpqffgVLHKdnONn84kibSjPGxGfkywxcIDAXucQvNy6ZJM-wGAdASro8nr1X1gLfBp0nC1tal5iF6xO3xRzL5OTizbtoLe2lC1QEisENoLB_DPHusXHrEv9bceBx00NhtZlnO2m6sRMj-eEQscLHJuYYa3uHq57mrDNNMB_X0z18jpuMoWi3i2x1aRl37AYUwnCRDPun6xqtJM4Roq_pLv8EZekr7Zt2sa_Nr-lIo0QnzDvkSxg-8rR3KKsCCE31AjOLA0uPWpk8k50wYVjmhqu_9mbH-tESNCqaQwaLFp4HAHskM
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><database>
<element label="API Point" max_length="40" name="x_snc_pointsthing_api_point" type="collection">
<element db_object_id="d47a3b43473d35104fb86285d36d4380" label="API Point" max_length="40" name="x_snc_pointsthing_api_point" type="collection">
<element label="Giver" max_length="32" name="giver" reference="x_snc_pointsthing_user" type="reference"/>
<element default="1" label="Points" max_length="40" name="points" type="integer"/>
<element label="Reason" max_length="40" name="reason" type="string"/>
<element label="Source" max_length="40" name="source" type="string"/>
<element label="Target" max_length="32" name="target" reference="x_snc_pointsthing_user" type="reference"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?><record_update>
<sys_dictionary action="INSERT_OR_UPDATE" element="points" table="x_snc_pointsthing_api_point">
<active>true</active>
<array>false</array>
<attributes/>
<audit>false</audit>
<calculation><![CDATA[(function calculatedFieldValue(current) {
// Add your code here
return ''; // return the calculated value
})(current);]]></calculation>
<choice/>
<choice_field/>
<choice_table/>
<column_label>Points</column_label>
<comments/>
<create_roles/>
<default_value>1</default_value>
<defaultsort/>
<delete_roles/>
<dependent/>
<dependent_on_field/>
<display>false</display>
<dynamic_creation>false</dynamic_creation>
<dynamic_creation_script/>
<dynamic_default_value/>
<dynamic_ref_qual/>
<element>points</element>
<element_reference>false</element_reference>
<foreign_database/>
<formula/>
<function_definition/>
<function_field>false</function_field>
<internal_type display_value="Integer">integer</internal_type>
<mandatory>false</mandatory>
<max_length>40</max_length>
<name>x_snc_pointsthing_api_point</name>
<next_element/>
<primary>false</primary>
<read_only>false</read_only>
<read_roles/>
<reference/>
<reference_cascade_rule/>
<reference_floats>false</reference_floats>
<reference_key/>
<reference_qual/>
<reference_qual_condition/>
<reference_type/>
<spell_check>false</spell_check>
<staged>false</staged>
<sys_class_name>sys_dictionary</sys_class_name>
<sys_created_by>SapphicFire</sys_created_by>
<sys_created_on>2023-10-16 09:05:30</sys_created_on>
<sys_name>Points</sys_name>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy/>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_update_name>sys_dictionary_x_snc_pointsthing_api_point_points</sys_update_name>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-16 09:05:30</sys_updated_on>
<table_reference>false</table_reference>
<text_index>false</text_index>
<unique>false</unique>
<use_dependent_field>false</use_dependent_field>
<use_dynamic_default>false</use_dynamic_default>
<use_reference_qualifier>simple</use_reference_qualifier>
<virtual>false</virtual>
<virtual_type>script</virtual_type>
<widget/>
<write_roles/>
<xml_view>false</xml_view>
</sys_dictionary>
</record_update>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?><record_update>
<sys_documentation element="points" label="Points" language="en" table="x_snc_pointsthing_api_point">
<sys_documentation action="INSERT_OR_UPDATE">
<element>points</element>
<help/>
<hint/>
<label>Points</label>
<language>en</language>
<name>x_snc_pointsthing_api_point</name>
<plural>Points</plural>
<sys_class_name>sys_documentation</sys_class_name>
<sys_created_by>SapphicFire</sys_created_by>
<sys_created_on>2023-10-16 09:05:30</sys_created_on>
<sys_mod_count>0</sys_mod_count>
<sys_name>Points</sys_name>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy/>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_update_name>sys_documentation_x_snc_pointsthing_api_point_points_en</sys_update_name>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-16 09:05:30</sys_updated_on>
<url/>
<url_target/>
</sys_documentation>
</sys_documentation>
</record_update>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?><record_update table="sys_scope_privilege">
<sys_scope_privilege action="INSERT_OR_UPDATE">
<operation>execute</operation>
<source_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</source_scope>
<status>allowed</status>
<sys_class_name>sys_scope_privilege</sys_class_name>
<sys_created_by>SapphicFire</sys_created_by>
<sys_created_on>2023-10-19 09:36:51</sys_created_on>
<sys_id>d9fa50e847c6f9104fb86285d36d4381</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_name>ScriptableServiceResultBuilder.setBody</sys_name>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy/>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_update_name>sys_scope_privilege_d9fa50e847c6f9104fb86285d36d4381</sys_update_name>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-19 09:36:51</sys_updated_on>
<target_name>ScriptableServiceResultBuilder.setBody</target_name>
<target_scope display_value="Global">global</target_scope>
<target_type>scriptable</target_type>
</sys_scope_privilege>
</record_update>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?><record_update sys_domain="global" table="sys_script">
<sys_script action="INSERT_OR_UPDATE">
<abort_action>false</abort_action>
<access>package_private</access>
<action_delete>false</action_delete>
<action_insert>true</action_insert>
<action_query>false</action_query>
<action_update>false</action_update>
<active>true</active>
<add_message>false</add_message>
<advanced>true</advanced>
<change_fields>false</change_fields>
<client_callable>false</client_callable>
<collection>x_snc_pointsthing_api_point</collection>
<condition/>
<description/>
<execute_function>false</execute_function>
<filter_condition/>
<is_rest>false</is_rest>
<message/>
<name>Create point from API</name>
<order>100</order>
<priority>100</priority>
<rest_method/>
<rest_method_text/>
<rest_service/>
<rest_service_text/>
<rest_variables/>
<role_conditions/>
<script><![CDATA[(function executeRule(current, previous /*null when async*/) {
for (var i = 0; i < current.points; i++){
if (current.getValue('giver') == current.getValue('target')) continue; // Still can't give yourself free points
var point = new GlideRecord("x_snc_pointsthing_point");
point.newRecord();
point.setValue('giver', current.getValue('giver'));
point.setValue('target', new x_snc_pointsthing.PointsThingAPI().establish_user(current.target.user_id, true, false, 'slack_id'));
point.setValue('api', current.getUniqueValue());
point.insert();
}
// Currently we aren't communicating API points explicitly, something to consider and add here if we go that way
})(current, previous);]]></script>
<sys_class_name>sys_script</sys_class_name>
<sys_created_by>SapphicFire</sys_created_by>
<sys_created_on>2023-10-16 09:47:07</sys_created_on>
<sys_domain>global</sys_domain>
<sys_domain_path>/</sys_domain_path>
<sys_id>f87e78e347f9b5104fb86285d36d4301</sys_id>
<sys_mod_count>5</sys_mod_count>
<sys_name>Create point from API</sys_name>
<sys_overrides/>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy/>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_update_name>sys_script_f87e78e347f9b5104fb86285d36d4301</sys_update_name>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-16 10:34:26</sys_updated_on>
<template/>
<when>async_always</when>
</sys_script>
<sys_translated_text action="delete_multiple" query="documentkey=f87e78e347f9b5104fb86285d36d4301"/>
</record_update>
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ PointsThing.prototype = {
if (response_body.user.name) {
var grupdate = new GlideRecord('x_snc_pointsthing_user');
grupdate.get(record_id);
var name = response_body.user.profile[gs.getProperty('x_snc_pointsthing.leaderboard_name')] ? response_body.user.profile[gs.getProperty('x_snc_pointsthing.leaderboard_name')] : response_body.user.profile['real_name']
var name = response_body.user.profile[gs.getProperty('x_snc_pointsthing.leaderboard_name')] ? response_body.user.profile[gs.getProperty('x_snc_pointsthing.leaderboard_name')] : response_body.user.profile['real_name'];
grupdate.setValue('user_name', name);
grupdate.setValue('name_updated', new GlideDateTime());
grupdate.update();
Expand Down Expand Up @@ -352,13 +352,13 @@ PointsThing.prototype = {
<sys_created_by>earl.duque</sys_created_by>
<sys_created_on>2022-10-13 03:22:36</sys_created_on>
<sys_id>88dd9085db629150791d8f8d13961998</sys_id>
<sys_mod_count>14</sys_mod_count>
<sys_mod_count>15</sys_mod_count>
<sys_name>PointsThing</sys_name>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy>read</sys_policy>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_update_name>sys_script_include_88dd9085db629150791d8f8d13961998</sys_update_name>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2023-06-06 14:35:52</sys_updated_on>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-16 10:08:27</sys_updated_on>
</sys_script_include>
</record_update>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?><record_update table="sys_script_include">
<sys_script_include action="INSERT_OR_UPDATE">
<access>public</access>
<active>true</active>
<api_name>x_snc_pointsthing.PointsThingAPI</api_name>
<caller_access/>
<client_callable>false</client_callable>
<description>Extension of the PointsThing include to support API-specific invocations</description>
<name>PointsThingAPI</name>
<script><![CDATA[var PointsThingAPI = Class.create();
PointsThingAPI.prototype = Object.extendsObject(x_snc_pointsthing.PointsThing,{
initialize: function() {
x_snc_pointsthing.PointsThing.prototype.initialize.call(this);
},
// Support name and ID, and don't do the name establish as any slack-facing call will ensure that
establish_user: function (payload_user, increment_point, set_points, userType) {
var userSysID = '';
var user = new GlideRecord('x_snc_pointsthing_user');
switch (userType){
case 'slack_id':
if(user.get('user_id',payload_user)){
user.getRowCount() > 1 ? gs.warn('User ID ' + payload_user + ' is present more than once in the list of PointsThing users') : null;
userSysID = user.getUniqueValue();
} else {
user.newRecord();
user.setValue('user_id', payload_user);
userSysID = user.insert();
}
break;
case 'name':
if(user.get('user_name',payload_user)){
user.getRowCount() > 1 ? gs.warn('Username ' + payload_user + ' is present more than once in the list of PointsThing users') : null;
userSysID = user.getUniqueValue();
} else {
user.newRecord();
user.setValue('user_name', payload_user);
userSysID = user.insert();
}
break;
default:
gs.warn('Unhandled payload_user value, ' + payload_user + ', provided. Falling back to ID handling');
if(user.get('user_id',payload_user)){
user.getRowCount() > 1 ? gs.warn('User ID ' + payload_user + ' is present more than once in the list of PointsThing users') : null;
userSysID = user.getUniqueValue();
} else {
user.newRecord();
user.setValue('user_id', payload_user);
userSysID = user.insert();
}
}
if (increment_point) {
var score = parseInt(user.getValue('points'));
score++;
user.setValue('points', score);
user.update();
} else if (set_points) {
if (user.getValue('points') != set_points) user.setValue('points', set_points);
user.update();
}
return userSysID;
},
type: 'PointsThingAPI'
});]]></script>
<sys_class_name>sys_script_include</sys_class_name>
<sys_created_by>SapphicFire</sys_created_by>
<sys_created_on>2023-10-16 10:19:48</sys_created_on>
<sys_id>d0f545ef47f9b5104fb86285d36d432a</sys_id>
<sys_mod_count>6</sys_mod_count>
<sys_name>PointsThingAPI</sys_name>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy>read</sys_policy>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_update_name>sys_script_include_d0f545ef47f9b5104fb86285d36d432a</sys_update_name>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-19 10:38:41</sys_updated_on>
</sys_script_include>
</record_update>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?><record_update table="sys_security_acl">
<sys_security_acl action="INSERT_OR_UPDATE">
<active>true</active>
<admin_overrides>true</admin_overrides>
<advanced>false</advanced>
<condition/>
<description/>
<local_or_existing>Local</local_or_existing>
<name>Points Thing Point Giving API</name>
<operation display_value="execute">execute</operation>
<script/>
<security_attribute/>
<sys_class_name>sys_security_acl</sys_class_name>
<sys_created_by>SapphicFire</sys_created_by>
<sys_created_on>2023-10-19 10:09:27</sys_created_on>
<sys_id>422264a4470af9104fb86285d36d4341</sys_id>
<sys_mod_count>1</sys_mod_count>
<sys_name>Points Thing Point Giving API</sys_name>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy/>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_update_name>sys_security_acl_422264a4470af9104fb86285d36d4341</sys_update_name>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-19 10:10:42</sys_updated_on>
<type display_value="REST_Endpoint">REST_Endpoint</type>
</sys_security_acl>
</record_update>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?><record_update table="sys_security_acl_role">
<sys_security_acl_role action="INSERT_OR_UPDATE">
<sys_class_name>sys_security_acl_role</sys_class_name>
<sys_created_by>SapphicFire</sys_created_by>
<sys_created_on>2023-10-19 10:09:27</sys_created_on>
<sys_id>003260a4470af9104fb86285d36d436d</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_name>.x_snc_pointsthing.api_giver</sys_name>
<sys_package display_value="Points Thing" source="x_snc_pointsthing">9c7718cddb229150791d8f8d13961979</sys_package>
<sys_policy/>
<sys_scope display_value="Points Thing">9c7718cddb229150791d8f8d13961979</sys_scope>
<sys_security_acl display_value="Points Thing Point Giving API">422264a4470af9104fb86285d36d4341</sys_security_acl>
<sys_update_name>sys_security_acl_role_003260a4470af9104fb86285d36d436d</sys_update_name>
<sys_updated_by>SapphicFire</sys_updated_by>
<sys_updated_on>2023-10-19 10:09:27</sys_updated_on>
<sys_user_role display_value="x_snc_pointsthing.api_giver" name="x_snc_pointsthing.api_giver">eae1ec64470af9104fb86285d36d430a</sys_user_role>
<transaction_id/>
</sys_security_acl_role>
</record_update>
Loading

0 comments on commit 2088ffe

Please sign in to comment.