Discourse plugin to restrict users from mentioning each other is failing - plugins

I have inherited a plugin used to restrict #mentions in discourse. Users are restricted to specific categories and are unable to view blocked categories, but when using the #mention in a topic the users in the restricted categories are showing up.
So user A works at company 1 and has access to the category associated to company 1. User B has access to the company 2 category. When user A #mentions someone on the company 1 category the autocomplete is displaying the users associated with the company 2 category.
I'm receiving no errors and the plugin supposedly worked before my arrival.
import { withPluginApi } from "discourse/lib/plugin-api";
import discourseComputed from "discourse-common/utils/decorators";
import userSearch from "discourse/lib/user-search";
function initWithApi(api) {
if (!Discourse.SiteSettings.restrict_mentions_enabled) return;
api.modifyClass("component:groups-form-interaction-fields", {
pluginId: 'groups-form-interaction-fields-plugin',
#discourseComputed(
"siteSettings.restrict_mentions_enabled",
"currentUser.admin",
"model.c_all_groups",
"model.name"
)
isShowRestrictMentions(enabled, admin, allGroups, name) {
return enabled && admin && allGroups && name && allGroups.includes(name);
},
#discourseComputed("model.c_all_groups", "model.name")
cSelectableGroups(allGroups, name) {
return (allGroups || []).filter(g => g !== name);
},
actions: {
setCAllowedMentionGroups(val) {
console.log(val);
let newVal;
if (val.includes("any")) {
newVal = "any";
} else {
newVal = val.filter(x => !Ember.isBlank(x)).join("|");
}
console.log(newVal)
this.model.set("c_allowed_mention_groups", newVal);
}
}
});
api.modifyClass("model:group", {
pluginId: 'group-plugin',
asJSON() {
const attrs = this._super(...arguments);
attrs["c_allowed_mention_groups"] = this.c_allowed_mention_groups;
return attrs;
},
#discourseComputed("c_allowed_mention_groups")
cAllowedMentionGroups(groups) {
return (groups || "").split("|");
}
});
api.modifyClass("component:composer-editor", {
pluginId: 'composer-editor-plugin',
userSearchTerm(term) {
if (!this.siteSettings.restrict_mentions_enabled) {
return this._super(...arguments);
}
let viewGroups = true;
const allowed =
this.get("topic.c_allowed_mention_groups") ||
this.currentUser.get("c_allowed_mention_groups");
console.log([this, allowed]);
if (Ember.isBlank(allowed)) {
return;
}
//REMOVING CUSTOMER GROUP FROM SEARCHABLE ARRAY OF STANDARD USERS
if(!this.currentUser.admin && !this.currentUser.moderator){
viewGroups = false;
const index = allowed.indexOf('All_Customers');
if (index > -1) {
allowed.splice(index, 1);
}
console.log(allowed)
}
const opts = {
term,
includeGroups: viewGroups,
groupMembersOf: allowed
};
return userSearch(opts);
}
});
}
export default {
name: "restrict-mentions",
initialize() {
withPluginApi("0.8", initWithApi);
}
};

Related

Updating data doesnt expand the data tree inside material-table

Im trying to build a table with nested tree folder inside.
When trying to add nested data into the datasource data the structure will not updated and will not toggle anymore.
Code below:
https://stackblitz.com/edit/angular-table-tree-example-k2zqmt?file=app%2Ftable-basic-example.ts&file=app%2Ftable-basic-example.html,app%2Ftable-basic-example.ts
Environment
Angular:
Material Table
Material tree system
These are the things that are happening when logNode method is called
The item is getting added but the treeControl.toggle method does not work anymore.
When you are assigning a new dataset to the dataSource all the nodes get reset and the tree closes, so this.treeControl.toggle is trying to toggle a node that does not exist.
You need to find the node to be toggled from the list you get from treeControl.dataNodes
I would suggest having the toggle code in a separate method and adding a node code in a separate method, and a separate button to add the node.
The below code should work for your scenario, also remove this line from your HTML, (click)="treeControl.toggle(data)"
interface ExampleFlatNode {
expandable: boolean;
RoleName: string;
Access: boolean;
level: number;
CatId: number;
}
private transformer = (node: FoodNode, level: number) => {
return {
expandable:
!!node.CategoryPermissions && node.CategoryPermissions.length > 0,
RoleName: node.RoleName,
Access: node.Access,
level: level,
CatId: node.CatId,
};
};
tempNodes = []
constructor() {
this.dataSource.data = TREE_DATA;
}
logNode(clickedNode) {
this.tempNodes = [];
this.treeControl.dataNodes.forEach((node) =>
this.tempNodes.push({
...node,
expanded: this.treeControl.isExpanded(node),
})
);
if (!this.treeControl.isExpanded(clickedNode)) {
const temp = {
Access: true,
RoleName: 'test 1 2',
CatId: 113,
};
const clickedNodeIdx = this.treeControl.dataNodes.findIndex(
(node: any) =>
node.CatId === clickedNode.CatId &&
node.RoleName === clickedNode.RoleName &&
node.level === clickedNode.level
);
const childIdx = 1;
let child;
if (clickedNode.level === 0) {
child =
this.dataSource.data[clickedNodeIdx].CategoryPermissions[childIdx];
} else {
this.dataSource.data.forEach(
(item) => (child = this.findDataSource(item, clickedNode))
);
}
child.CategoryPermissions.push(temp);
this.dataSource.data = this.dataSource.data;
const addedNode = this.treeControl.dataNodes.find(
(node: any) =>
node.CatId === temp.CatId && node.RoleName === temp.RoleName
);
this.expandParent(addedNode);
this.setPreviousState();
} else {
this.treeControl.collapse(clickedNode);
}
}
findDataSource(item, node) {
if (item.RoleName === node.RoleName) {
return item;
} else if (item.CategoryPermissions) {
let matchedItem;
item.CategoryPermissions.forEach((e) => {
const temp = this.findDataSource(e, node);
if (temp) {
matchedItem = temp;
}
});
return matchedItem;
}
}
setPreviousState() {
for (let i = 0, j = 0; i < this.treeControl.dataNodes.length; i++) {
if (
this.tempNodes[j] &&
this.treeControl.dataNodes[i].RoleName === this.tempNodes[j].RoleName &&
this.treeControl.dataNodes[i].CatId === this.tempNodes[j].CatId &&
this.treeControl.dataNodes[i].level === this.tempNodes[j].level
) {
if (this.tempNodes[j].expanded) {
this.treeControl.expand(this.treeControl.dataNodes[i]);
}
j++;
}
}
}
expandParent(node: ExampleFlatNode) {
const { treeControl } = this;
const currentLevel = treeControl.getLevel(node);
const index = treeControl.dataNodes.indexOf(node) - 1;
for (let i = index; i >= 0; i--) {
const currentNode = treeControl.dataNodes[i];
if (currentLevel === 0) {
this.treeControl.expand(currentNode);
return null;
}
if (treeControl.getLevel(currentNode) < currentLevel) {
this.treeControl.expand(currentNode);
this.expandParent(currentNode);
break;
}
}
}

swashbuckle openapi 3 write example and description for the dynamically generated model classes

My model properties definition is coming from a json file so using reflection to write the classes to be shown under schema on resulting swagger page.
foreach (var model in Models)
{
if (!ModelTypes.ContainsKey(model.Key))
{
anyNonCompiledModel = true;
BuildModelCodeClass(modelComponentBuilder, model.Value);//Build model classes
}
}
BuildModelCodeEnd(modelComponentBuilder);
if (anyNonCompiledModel)
{
CSharpCompiler compiler = new CSharpCompiler();
compiler.AddReference(typeof(object));
compiler.AddReference(typeof(ResourceFactory));
compiler.AddReference(typeof(System.Runtime.Serialization.DataContractResolver));
compiler.AddReference(typeof(System.Runtime.Serialization.DataContractAttribute));
var types = compiler.Compiler(modelComponentBuilder.ToString()); //write model classes
foreach (var type in types)
{
ModelTypes.Add(type.Name, type);
}
}
public void BuildModelCodeClass(StringBuilder modelComponentBuilder, MetadataModelEntity model)
{
modelComponentBuilder.AppendLine($"public class {model.Name} {{");
foreach (var p in model.Data.Properties)
{
if (p.Obsoleted) continue;
if (p.Type.Type == "array")
{
modelComponentBuilder.AppendLine($" public {p.Type.ArrayType.ObjectName}[] {p.Name} {{get;set;}}");
}
else
{
//primitive types
modelComponentBuilder.AppendLine($" public {p.Type.ObjectName} {p.Name} {{get;set;}}");
}
}
modelComponentBuilder.AppendLine(
#"}
");
}
If i provide the description and example like following (in BuildModelCodeClass, inside the loop) then the example and description displays for me.
if (!string.IsNullOrWhiteSpace((string)p.Example))
{
modelComponentBuilder.AppendLine($" ///<example>{p.Example}</example>");
}
if (!string.IsNullOrWhiteSpace((string)p.Description))
{
modelComponentBuilder.AppendLine($" ///<description>{p.Description}</description>");
}
However, i dont want to do above.
I want to write my models via the open api and not via the C# Compiler, is it possible?
I want to show example and description via schema (may be under paths some where). How can i do this? Context has my models info available that i can interact with here.
public class SwaggerDocumentFilter : IDocumentFilter
{
SwaggerDocument _swaggerDocument;
public SwaggerDocumentFilter(object apiConfigure)
{
_swaggerDocument = ((ApiGatewayConfiguration)apiConfigure).SwaggerDocument;
}
public void Apply(OpenApiDocument document, DocumentFilterContext context)
{
if (document.Info.Extensions == null || !document.Info.Extensions.ContainsKey(SwaggerEndpoint.ExtensionDocName)) return;
var openIdString = document.Info.Extensions[SwaggerEndpoint.ExtensionDocName] as OpenApiString;
if (openIdString == null) return;
var docName = openIdString.Value;
SwaggerEndpoint endpoint = _swaggerDocument.SwaggerEndpoints.SingleOrDefault(x => x.Name == docName);
if (endpoint == null) return;
//Add server objects
document.Servers = endpoint.ServerObjects;
//Add Tags objects
document.Tags = endpoint.Tags;
//Set swagger paths objects
var pathsObjects = _swaggerDocument.GetPathsObject(docName, context);
if (pathsObjects.IsValid())
{
pathsObjects.ToList().ForEach(
item => document.Paths.Add(item.Key, item.Value)
);
}
//Add Schema components
//Add Example/Examples
}
}
Following helped
https://github.com/domaindrivendev/Swashbuckle.WebApi/issues/162
AddSchemaExamples.cs
public class AddSchemaExamples : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
if (type == typeof(Product))
{
schema.example = new Product
{
Id = 123,
Type = ProductType.Book,
Description = "Treasure Island",
UnitPrice = 10.0M
};
}
}
}
SwaggerConfig.cs
httpConfig
.EnableSwagger(c =>
{
c.SchemaFilter<AddSchemaExamples>()
});
My implementation for the Apply since model is dynamic
if (model != null)
{
schema.Description = model.Description;
foreach (var p in schema.Properties)
{
var mp = model.Data.Properties.SingleOrDefault(x => x.Name == p.Key);
if (mp != null)
{
if (!string.IsNullOrWhiteSpace(mp.Description))
{
p.Value.Description = mp.Description;
}
if(!string.IsNullOrWhiteSpace(mp.Example))
{
p.Value.Example =
new Microsoft.OpenApi.Any.OpenApiString(mp.Example.ToString());
}
}
}
}

Svelte doesn't reorder Meteor collection

My Svelte component is set up to find all people in a MongoDB document and list them in a table. When clicking on a column header the collection should sort by that column/field and it should toggle the sort direction with subsequent clicks.
My script section:
$: sortColumn = 'name';
$: sortDirection = 'asc';
$: sortParameters = setSortParams(sortColumn, sortDirection);
$: contactList = [];
function getContactList(sortObj) {
contactList = Contacts.find({
isBlocked: false,
isDeleted: { $ne: true }
},
{
sort: sortObj
}).fetch();
contactList = contactList;
}
onMount(() => {
getContactList(setSortParams(sortColumn, sortDirection));
});
function changeSortDirection() {
if (sortDirection === 'asc') {
sortDirection = 'desc';
} else {
sortDirection = 'asc';
}
}
function sortByColumn(col) {
if (col === sortColumn) {
changeSortDirection();
} else {
sortDirection = 'asc';
}
sortColumn = col;
getContactList(setSortParams(sortColumn, sortDirection));
}
function setSortParams(sortField, sDirection) {
let sortParams = [];
let direction = sDirection || 1;
let field = sortField || 'name';
if (direction === 'asc') {
direction = 1;
} else {
direction = -1;
}
if (field === 'name') {
sortParams.push(['firstName', direction]);
sortParams.push(['lastName', direction]);
} else {
sortParams.push([field, direction]);
}
sortParams = sortParams;
return sortParams;
}
And the relevant part of my svelte file:
{#each columns as column}
<th class="contact-table__column contact-table__column-header"
on:click={() => sortByColumn(column.type)}>
<span class="contact-table__title">{column.display} {sortDirection}</span>
</th>
{/each}
The collection reorders when I click on a different column header, but it doesn't reorder when I click on the same header (it should switch between ASC and DESC sort order).
I'm new to Svelte and Meteor so I'm sure there's a few things I'm doing wrong. I appreciate any help.

How to set expiration of newsletter confirmation link in magento 2

I have newsletter in my magento website. I have enable confirmation before subscription from admin configuration. User are getting confirmation link in mail.
But I want to set expiry of that link. Is magento provide default config ?
How can I set expiry of that link ?
I found the solution. I just did two things.
1) Add created_at field in newsletter_subscriber table.
2) Overwrite the following file
vendor/magento/module-newsletter/Model/Subscriber.php
to
Company/name/Model/Subscriber.php
Overited Subscriber.php file code
public function confirm($code) // existing function
{
$id = $this->getId();
if ($this->validateConfirmLinkToken($id, $code)) {
if ($this->getCode() == $code) {
$this->setStatus(self::STATUS_SUBSCRIBED)
->setStatusChanged(true)
->save();
$this->sendConfirmationSuccessEmail();
return true;
}
return false;
}
}
private function validateConfirmLinkToken($customerId, $code) //check validation for token
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$messageManager = $objectManager->get('Magento\Framework\Message\ManagerInterface');
if (empty($customerId) || $customerId < 0) {
$this->_messageManager->addError('Sorry you have not rigts to access this page');
return false;
}
if (!is_string($code) || empty($code)) {
$params = ['fieldName' => 'code'];
//$messageManager->addError('Sorry Your subscription confirmation code is not valid.');
return false;
}
$dcode = $this->getCode();
$dcreated_at = $this->getCreatedAt();
if (trim($dcode) != trim($code)) {
//$messageManager->addError('Sorry Your subscription confirmation code is mismatch.');
return false;
} elseif ($this->isConfirmationLinkTokenExpired($dcode, $dcreated_at)) {
//$messageManager->addError('Sorry Your subscription confirmation code is expired.');
return false;
}
return true;
}
public function isConfirmationLinkTokenExpired($dcode, $dcreated_at) // check expiration token
{
if (empty($dcode) || empty($dcreated_at)) {
return true;
}
$expirationPeriod = '720';
$currentTimestamp = (new \DateTime())->getTimestamp();
$tokenTimestamp = (new \DateTime($dcreated_at))->getTimestamp();
if ($tokenTimestamp > $currentTimestamp) {
return true;
}
$hourDifference = floor(($currentTimestamp - $tokenTimestamp) / (60 * 60));
if ($hourDifference >= $expirationPeriod) {
return true;
}
return false;
}
Hope it will helps to many.
Thanks.

Silverstripe 3.1.x getting values from Parent

I would like to get a generic function working for getting Data in Template of a Page and if the property is not set, getting it from the Parent or Parents Parent and so on. With generic I mean independent of Relations like db, HasOne, HasMany, ManyMany. Let's say I have this for ManyMany but would like to detect if it's a Object, HasManyList or ManyManyList or a value. Is anything like this built in or how would you go about?
function ManyManyUpUntilHit($ComponentName){
$Component = $this->getManyManyComponents($ComponentName);
if($Component && $Component->exists())
return $Component;
$Parent = $this->Parent();
if(is_object($Parent) && $Parent->ID != 0){
return $Parent->ManyManyUpUntilHit($ComponentName);
} else {
return null;
}
}
in template:
$ManyManyUpUntilHit(Teaser)
There is no in built method to do this in Silverstripe. You will need to write your own function to do this.
Here is an example to get a page's has_one, has_many or many_many resource by parameter, or go up the site tree until a resource is found, or we hit a root page:
function getComponentRecursive($componentName) {
// has_one
if ($this->has_one($componentName)) {
$component = $this->getComponent($componentName);
if (isset($component) && $component->ID)
{
return $component;
}
}
// has_many
if ($this->has_many($componentName)) {
$components = $this->getComponents($componentName);
if (isset($components) && $components->count())
{
return $components;
}
}
// many_many
if ($this->many_many($componentName)) {
$components = $this->getManyManyComponents($componentName);
if (isset($components) && $components->count())
{
return $components;
}
}
if ($this->ParentID != 0)
{
return $this->Parent()->getComponentRecursive($componentName);
}
return false;
}